Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Does deep JSON serialization work? #44

Closed
1cg opened this issue Jun 8, 2011 · 2 comments
Closed

Does deep JSON serialization work? #44

1cg opened this issue Jun 8, 2011 · 2 comments
Assignees

Comments

@1cg
Copy link
Collaborator

1cg commented Jun 8, 2011

Looking at the code, I don't think we recurse on objects that come in as json members. Some interns at GW were having issues around that.

@ghost ghost assigned kprevas Jun 9, 2011
@kprevas
Copy link
Owner

kprevas commented Jun 9, 2011

You're right, we don't go more than one level deep. I can look in to this.

@1cg
Copy link
Collaborator Author

1cg commented Jun 9, 2011

It'd be nice if you could either/or it: either embed an ID in the "fromID" attribute or, if that isn't present, then new up the object and reflect.

Here is the code that I sent to the interns, which appears to kind of work:

function deserializeJSON( str : String, rootType : Type ) : T {

var x = new JSONReader()

var root = x.read( str )

return transformObj( root, rootType ) as T

}

function transformObj( val : Object, type : Type ) : Object {

if( val typeis Map ) {

var instance = type.TypeInfo.getConstructor( {} ).Constructor.newInstance( {} )

val.eachKeyAndValue( \ propName, propVal -> {

  var prop = type.TypeInfo.getProperty( propName as String )

  if( prop.Writable ) {

    prop.Accessor.setValue( instance, CommonServices.getCoercionManager().convertValue( transformObj( propVal, prop.FeatureType ), prop.FeatureType ) )

  }

})

// maybe commit object here?

return instance

} else if( val typeis List ) {

return val.map( \ o -> transformObj( o, type.TypeParameters.first() )  )

} else {

print( "expected: ${type.Name}, found ${typeof val}" )

return val

}

}

Obviously it needs to be more robust (list handling is very raw, error handling is non-existant, should we commit the db objects to the db up front? If not, how can a user figure out what to commit?) but at least the reflection appears to work reasonably well.

Cheers,
Carson

On Jun 9, 2011, at 8:20 AM, kprevas wrote:

You're right, we don't go more than one level deep. I can look in to this.

Reply to this email directly or view it on GitHub:
#44 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants