Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs

How to encode ds_map like json_encode() ?

A topic by sjyoooon created 72 days ago Views: 41 Replies: 2
Viewing posts 1 to 2

Hi

I tried encode ds_map to json string like follow:


var map = ds_map_create();

ds_map_add(map, "a", 10);

ds_map_add(map, "b", "50");

var json_string = tj_encode(map);

show_debug_message( "result: "+ json_string ); 


and the result was :

 result: 414

How can I fix this?

And  what does ?indent mean?

Developer

As per documentation, tj_encode accepts arrays, TJ objects, TJ booleans, and basic values. It does not accept maps, as there isn't even a way to tell that your value is a map, let alone tell which children of it might be maps.

So you'd want to do

var obj = tj_object("a",10, "b",50);
var json_string = tj_encode(map);
show_debug_message( "result: "+ json_string );

or

var obj = tj_object();
tj_set(obj, "a", 10);
tj_set(obj, "b", 50);
var json_string = tj_encode(map);
show_debug_message( "result: "+ json_string );
if you can describe your use case in more detail, I may have further recommendations.

Thank you for rapid reply.  Second one is exactly what I wanted!

What are differences between ds_map and tj_object?

Is it okay to replace all my maps to tj_object?