Skip to content

Commit

Permalink
Add toJSONString to JSONTreeNode
Browse files Browse the repository at this point in the history
  • Loading branch information
humdrum committed Oct 19, 2023
1 parent d80ad48 commit d064f9d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Sources/Document/Json/JSONTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Foundation

public protocol JSONTreeNode: Equatable {
var type: TreeNodeType { get }
var toJSONString: String { get }
}

struct TreeChangeWithPath {
Expand Down Expand Up @@ -86,6 +87,39 @@ public struct JSONTreeElementNode: JSONTreeNode {
self.attributes = attributes
self.children = children
}

public var toJSONString: String {
var childrenString = ""
if self.children.isEmpty == false {
childrenString = self.children.compactMap { $0.toJSONString }.joined(separator: ",")
}

var resultString = "{\"type\":\"\(self.type)\",\"children\":[\(childrenString)]"

if self.attributes.isEmpty == false {
let sortedKeys = self.attributes.keys.sorted()

let attrsString = sortedKeys.compactMap { key in
if let value = attributes[key] as? Int {
return "\"\(key)\":\(value)"
} else if let value = attributes[key] as? Double {
return "\"\(key)\":\(value)"
} else if let value = attributes[key] as? Bool {
return "\"\(key)\":\(value)"
} else if let value = attributes[key] as? String {
return "\"\(key)\":\"\(value)\""
} else {
return "\"\(key)\":null"
}
}.joined(separator: ",")

resultString += ",\"attributes\":{\(attrsString)}"
}

resultString += "}"

return resultString
}
}

/**
Expand All @@ -98,6 +132,10 @@ public struct JSONTreeTextNode: JSONTreeNode {
public init(value: String) {
self.value = value
}

public var toJSONString: String {
return "{\"type\":\"\(self.type)\",\"value\":\"\(self.value)\"}"
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions Tests/Unit/Document/JONSTreeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import XCTest
@testable import Yorkie

final class JONSTreeTests: XCTestCase {
func test_json_string() throws {
let textNode = JSONTreeTextNode(value: "X")

XCTAssertEqual(textNode.toJSONString, "{\"type\":\"text\",\"value\":\"X\"}")

let elementNode = JSONTreeElementNode(type: "doc",
attributes: [
"intValue": 100,
"doubleValue": 10.5,
"boolean": true,
"string": "testString"
],
children: [
JSONTreeElementNode(type: "p"),
JSONTreeTextNode(value: "Y")
])

XCTAssertEqual(elementNode.toJSONString, "{\"type\":\"doc\",\"children\":[{\"type\":\"p\",\"children\":[]},{\"type\":\"text\",\"value\":\"Y\"}],\"attributes\":{\"boolean\":true,\"doubleValue\":10.5,\"intValue\":100,\"string\":\"testString\"}}")
}
}

0 comments on commit d064f9d

Please sign in to comment.