Converting a SPOD Macro object to JSON string

99 views
Skip to first unread message

Adrian Martinez

unread,
Feb 18, 2013, 8:25:04 AM2/18/13
to haxe...@googlegroups.com
Hi all,

I'm trying to stringify a SPOD Macro object to Json so I can pass it's data to javascript:


var table = new MyTableObject();
MyTableObject.someField = "Hello world!";

trace
( Json.stringify( table ) );


But php gives me than this error: Fatal error: Access to undeclared static property: bkh_database_MyTableObject::$__tname__ in /Applications/MAMP/htdocs/Open-Projects/Boekhouding/Web/bin/web/php/php/Boot.class.php(303) : eval()'d code on line 1

I also tried using Serialization, but that also fails when doing: Serializer.run( table );

Does someone know how I could fix this, my main goal is to pass the data of the spod object to javascript.

Regards, Adrian

Juraj Kirchheim

unread,
Feb 18, 2013, 9:23:44 AM2/18/13
to haxe...@googlegroups.com
The problem is basically that SPOD objects carry around a lot of
"junk" related to SPOD. You will first have to filter that out by
either using a custom serialization (__serialize or something) or
replacer (requires a recent nightly build and off the top of my head I
am not sure you can actually skip fields), or create an anonymous
object with the data you want (can be done with a macro to save time
;)).

Regards,
Juraj

Adrian Martinez

unread,
Feb 18, 2013, 10:44:38 AM2/18/13
to haxe...@googlegroups.com

Hi Juraj,

I'm not sure how I could apply a custom serialization.

I looked already at the docs but I'm not sure how I could apply that to spod objecs. Would I have to make a costum serializator for each spod object, or just one for all spod objects.


At the moment I have a temporary noobish solution that works but it's not very efficient.


The code fails when accessing one of the following fields:

_manager

_lock

__cache__


So I skip them inside a loop since their names are prefixed by an underscore:



private function toJson( tableData : List<Dynamic> ) : String
{
var jsonTable : Array<Dynamic> = [];
for( rowData in tableData )
{
var jsonRow : Dynamic = {};
for( fieldName in Reflect.fields( rowData ) )
{
if( fieldName.substr( 0, 1 ) != "_" )
{
Reflect.setField( jsonRow, fieldName, Reflect.field( rowData, fieldName ) );
}
}
jsonTable.push( jsonRow );
}
return Json.stringify( jsonTable );
}
 

Regards, Adrian


Op maandag 18 februari 2013 15:23:44 UTC+1 schreef back2dos het volgende:

Juraj Kirchheim

unread,
Feb 18, 2013, 12:57:47 PM2/18/13
to haxe...@googlegroups.com
In principle, there's nothing wrong with your approach. I wouldn't
worry so much about efficiency, because this kind of transformation is
not extensively expensive compared to all the rest that is going on
(database call -> query operation -> serialization of result set ->
transmission to server -> deserialization of result set -> wrapping of
result set into SPOD objects -> your tranformation -> json encoding ->
transmission to client -> json decoding).

If this becomes your app's bottleneck, then it would be best to bypass
SPOD, but I highly doubt it.

A more important question is, how exactly relations fit into all of
this. If you have arbitrarily complex data, you will need your cleanup
function to be recursive or to skip relations.

Regards,
Juraj

david quertelet

unread,
Feb 19, 2013, 2:58:56 AM2/19/13
to haxe...@googlegroups.com
i think i've seen in an old mailing list that nicolas cannasse suggested  to serialize the __cache__ object for your needs!
--
To post to this group haxe...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "Haxe" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haxelang+u...@googlegroups.com.

Adrian Martinez

unread,
Feb 19, 2013, 8:46:32 AM2/19/13
to haxe...@googlegroups.com
Thnx for the explanation Juraj.
I will use this method then.
Once I'll find a better solution I can always update my code.
But for now it works, so I'm happy :)

Regards, Adrian

Op maandag 18 februari 2013 18:57:47 UTC+1 schreef back2dos het volgende:

Adrian Martinez

unread,
Feb 19, 2013, 8:51:01 AM2/19/13
to haxe...@googlegroups.com, da...@postite.com
Hi David, I don't know how to do a custom serialization.
I looked at the docs but it's quite vague to me.

Op dinsdag 19 februari 2013 08:58:56 UTC+1 schreef david quertelet het volgende:

david quertelet

unread,
Feb 19, 2013, 8:57:28 AM2/19/13
to haxe...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages