Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

How to encode ds_map like json_encode() ?

A topic by sjyoooon created Sep 28, 2019 Views: 591 Replies: 5
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?

I didn't know that tj_set() can add new  values. 

I  found  out about this here.

Without that it was difficult to  work with the  extension.

Please add this info to the documentation for other people ...

Great extension!

Developer

tj_set is equivalent to ds_map_set, adding a new key-value pair or replacing an existing one. I'll see about updating the docs to be clearer.

Thanks! Your extensions are huge time savers :)