loganwright / Genome

A simple, type safe, failure driven mapping library for serializing JSON to models in Swift 3.0 (Supports Linux)
MIT License
762 stars 58 forks source link

Problem with restoring data #64

Closed ghost closed 8 years ago

ghost commented 8 years ago

I have a custom data structure, with DM_ModelRoot as root item. Writing to a JSON string is working fine:

let map = Map()
var settableRoot: DM_ModelRoot? = self.modelRoot

try settableRoot ~> map["modelRoot"]

print(settableRoot)

var jsonString = map.toJson.serialize()

However, when trying to restore the JSON string, there is an error UnableToMap(.KeyPath(modelRoot), Genome.SequenceError.FoundNil("Key: .KeyPath(identifier) TargetType: String")):

var settableRoot: DM_ModelRoot?

let json = try Json.deserialize(jsonString)

let map = Map(json: json)

try settableRoot <~ map["modelRoot"]

What am I doing wrong?

Note: DM_ModelRoot is setup with init(...) and sequence(...) properly, the JSON string is created correctly.

BTW: It would be helpful to have examples for correctly reading and writing JSON from and into custom objects on the documentation page ;-)

ghost commented 8 years ago

BTW: There seems to be a bug in the error message - shouldn't identifier be a value? FoundNil("Key: .KeyPath(identifier) TargetType: String")

loganwright commented 8 years ago

@innoreq sorry I missed this one, and I'm getting here so late. Can you post the model code?

ghost commented 8 years ago

Thanks for your feedback. Unfortunately, I cannot reconstruct the problem anymore, as we changed from JSON to a database solution in the meantime.

loganwright commented 8 years ago

@innoreq thanks, I'll close this for now.