Is there a JsonObject native mapped type in Cocoa/Java?
I mean supposed that I create methods like:
/**
* Map json string to object
*/
public func map(jsonString:String!) ->() {
let myObject:Sugar.Json.JsonObject = Sugar.Json.JsonObject.Load( jsonString );
if let obj = myObject {
let allKeys = obj.Keys;
for key in allKeys {
if let value = obj.Item[key] {
// do something here...
}
}
self.rawObject=Sugar.Json.JsonObject.Load( myObject.ToJson() );
}
}
where I have
/**
* Base JSON Object Model
*/
public class BaseObject {
private var rawObject:Sugar.Json.JsonObject?;
public init() {
super.init();
}
and I would like to return this whole self.rawObject as a native JSON object (Cocoa/Java) to the caller, is there any public interface api (the ones mapped in https://github.com/remobjects/sugar/blob/master/Sugar/JSON/JsonObject.pas) to get back a native object in Cocoa so Swift.Dictionary (so using native json parsers objects) like:
let epoch:NSTimeInterval = NSDate().timeIntervalSince1970;
let jsonObject : [String:AnyObject] = [
"timestamp" : epoch
];
print("\(jsonObject["timestamp"])");
in order that I could both do NSJSONSerialization like:
do {
let data:NSData = try NSJSONSerialization.dataWithJSONObject(jsonObject, options: NSJSONWritingOptions.PrettyPrinted);
print("\(data)");
let jsonObj2:AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments);
print("\(jsonObj2)");
} catch _ {
}
/**
* Get Json Object
*/
public func toJsonObject() -> ([String:AnyObject]?) {
var json:[String:AnyObject] = [String:AnyObject]();
if let obj = self.rawObject {
let allKeys = obj.Keys;
for key in allKeys {
if let value = obj.Item[key] {
json[key]=value;
}
}
return json;
}
return nil;
}
to map the Sugar.JSON.JsonObject into a Swift.Dictionary like structure in order to back a json like the following:
if let jsonObject=result.toJsonObject() { // json object
print("\(jsonObject)\n\(jsonObject["artists"])");
if let jsonValue=jsonObject["artists"] {
print("\(jsonValue["href"])");
}
}
but I’m only traversing the first nodes/root level, since at second level ( i.e. [‘artists’]) I get a Sugar.JsonValue when doing let value = obj.Item[key]. and so only one level is returned as a Swift json object:
print("\(jsonObject)\n\(jsonObject["artists"])");
but not inners levels like jsonObject["artists"]!["href"]
Items: Dictionary<String, JsonNode> := new Dictionary<String, JsonNode>;
is private. Now supposed it would be public I could access to Sugar.Dictionary there. And since a Sugar.Dictionary is a mapped type. I could use it as-it-is in Cocoa, right?
I see. i suppose the Sugar.Json API just could use some improving to make that possible w/o having to cast. right now, jsonObject["item"] is a JsonNode, not a JsonObject, so you cant directly access the sub-properties. i’ll think about this some more.
@mh Yes exactly, having a jsonObject["item"] like a JsonObject will definitively solve the problem, since one could traverse each object through the Keys (if any) and then map into a dictionary.
Thank you.