Closed diegomontoyas closed 8 years ago
Hi @diegomontoyas
If you have raw data, it's a little easier. Unfortunately, PureJsonSerializer isn't 100% up to spec and isn't actively maintained, so we need to make it a little trickier. Here's how you'd do it using Foundation's apis.
let jsonString = "{\"hello\": \"json\"}"
let data = Data(bytes: Array(jsonString.utf8))
let js = try JSONSerialization.jsonObject(
with: data,
options: .init(rawValue: 0)
)
Although, it might be worthwhile using data direct in your model:
let jsonString = "{\"hello\": \"json\"}"
let data = Data(bytes: Array(jsonString.utf8))
let model = try YourModel(node: data)
Let me know if you have any more questions.
Hi Logan, thank you very much for the response. I asked purely based on a performance perspective. I temporarily fixed it by using Foundation's JSONSerialization, however this implies that I have to do something like this:
let any = try JSONSerialization.jsonObject(with: data, options: [])
let node = Node(any: any)
which does twice the work, parsing data to Foundation types, and then parsing this "Any" to a Node instance.
PureJsonSerializer parsed a raw string directly, without passing through foundation types first, so I wanted this type of nice direct conversion.
From your response I see now that Data is a NodeRepresentable, but the implementation of makeNode
is exactly the same as above, which would represent a performance cost. This is why I wanted to see if there was a direct conversion as we had with PureJsonSerializer. I haven't measured if the impact would be actually noticeable in everyday use, however.
@diegomontoyas you are correct, there is likely a small performance hit with the casting, but we've had pretty comparable results w/ Foundation on osx, I imagine it will not be a large issue. Unfortunately, there are aspects of PureJsonSerializer that weren't properly up to spec and I didn't have time to properly assess the underlying issues in the parser so I couldn't ship it w/ Genome.
If you'd like to use a pure swift parser, I highly recommend Jay. It's easy to make it all work w/ Genome, if you'd like to go that route.
It would require a cocoapod, but essentially, once you have jay, you could do something like this:
extension Jay.JSON: NodeRepresentable {
public func makeNode() -> Node {
// do the conversion here in an explicit way w/o casting.
}
}
Then just use Jay.JSON
anywhere you'd want to. Take a look at this library as well if it's helpful.
When SPM has great iOS support, I'd be very open to integrating with this library, but the dependency tree is quite a bit of work to support currently so it'll require manual integration from users.
Perfect @LoganWright. It's ok, thank you very much for the response! I'll take a look at that.
Hi! Yesterday I serialized and deserialized strings to and from JSON directly with PureJsonSerializer like this:
Json.deserialize(rawString)
andJson.serialize(aStyle)
but know I'm a little but confused as to how to do it with Node. Could you give us some insight on this please?
Thanks!