gonzalezreal / Groot

From JSON to Core Data and back.
Other
534 stars 61 forks source link

Serialised output field names not in quotes #89

Closed firvorski closed 6 years ago

firvorski commented 6 years ago

Hi, I wondered if anyone might have a suggestion as to what I might be doing wrong here.

When I try to serialise my core data objects, in the output I get the field names (and some string values too) do not have any quotes around them i.e. do not appear to be strings. If I then try to import a file with JSON data formatted in this way it fails.

All name and id fields are strings. The other objects are integers or binary.

let results = json(fromObjects: logs)
for res in results {
     pr("\(res)")
}

The output

{ duration = 1200; id = "665AC217-4E13-4CDA-8007-728FF7567B2C"; isCompleted = 1; pomodoro = { category = { id = "AF8CE7E9-07D3-4B26-9578-7035F1B07686"; name = Wealth; }; id = "4BB989FF-E253-4D7E-BC66-A8C113AC4A5A"; name = "Export plist"; project = { category = { id = "AF8CE7E9-07D3-4B26-9578-7035F1B07686"; name = Wealth; }; id = "7AE74AF8-2686-46ED-9673-C1384226FB45"; name = Pomodorio; tags = ( ); }; type = { id = "5E79BB6F-F626-42A8-A303-5143C48F2F50"; name = Coding; }; }; timestamp = "2017-11-25 23:57:00 +0000"; }

gonzalezreal commented 6 years ago

Hi,

The type returned by json(fromObjects:) is an array of dictionaries that can be converted to JSON using JSONSerialization. So you probably want to do this:

let results = json(fromObjects: logs)
let data = try! JSONSerialization.data(withJSONObject: results)
let json = String(data: data, encoding: .utf8)!
print(json)
firvorski commented 6 years ago

Great! It's working now. Thanks a lot.