1. EuMVC:Map to Json
- Posted by euphoric (admin) Jan 06, 2022
- 895 views
@ghaberek, or anybody who knows,
Is there a map_to_json() available in Greg's json lib?
(If not, there should be. )
Also, if not, how would I do it?
Thank you!
2. Re: EuMVC:Map to Json
- Posted by ghaberek (admin) Jan 07, 2022
- 911 views
@ghaberek, or anybody who knows,
Is there a map_to_json() available in Greg's json lib?
(If not, there should be. )
Also, if not, how would I do it?
Thank you!
You're right, there should be, but Euphoria isn't typed strongly enough to make a one-to-one translation from a map to JSON, so some type information has to be specified manually.
I originally did try storing JSON objects as maps but I had no way of reading back value types beyond "atom or sequence" and since maps are just atoms I couldn't tell a regular number from any other JSON type.
This is why I opted to just reference everything with its type as {type,value}. Another snag is that Euphoria maps accept any key, but JSON keys can only be strings, so there are significant limits with trying to convert maps to JSON.
That being said, I just pushed v1.15.0 which includes a new json_import() function just for you (complete with docs). I've posted a simple test program below.
(P.S. this also helped me find a bug in json_print() that would totally screw up numbers by printing them with unnecessary scientific notation!)
include std/map.e include std/types.e include mvc/json.e map color_map = map:new_from_kvpairs({ { "black", #000000 }, { "maroon", #800000 }, { "green", #008000 }, { "olive", #808000 }, { "navy", #000080 }, { "purple", #800080 }, { "teal", #008080 }, { "silver", #C0C0C0 }, { "grey", #808080 }, { "red", #FF0000 }, { "lime", #00FF00 }, { "yellow", #FFFF00 }, { "blue", #0000FF }, { "fuchsia", #FF00FF }, { "aqua", #00FFFF }, { "white", #FFFFFF } }) procedure main() object json = json_import( color_map ) if json[J_TYPE] = JSON_NONE then printf( 2, "%s\n", {json_last_error()} ) end if json_print( 1, json,, TRUE ) end procedure main()
Output are integers because JSON doesn't support hexadecimal notation.
{ "aqua": 65535, "black": 0, "blue": 255, "fuchsia": 16711935, "green": 32768, "grey": 8421504, "lime": 65280, "maroon": 8388608, "navy": 128, "olive": 8421376, "purple": 8388736, "red": 16711680, "silver": 12632256, "teal": 32896, "white": 16777215, "yellow": 16776960 }
-Greg
3. Re: EuMVC:Map to Json
- Posted by petelomax Jan 07, 2022
- 862 views
You're right, there should be, but Euphoria isn't typed strongly enough to make a one-to-one translation from a map to JSON, so some type information has to be specified manually.
What I did, at least when parsing json and not that it has anything to do with maps, dunno if this will help:
•Numbers and Strings (without quotes) are held natively
•An object is held as a sequence with a first element of -1 (JSON_OBJECT),and the rest pairs, the first of which is a string and the second any of these types.
•An array is held as a sequence with a first element of -2 (JSON_ARRAY),and the rest any of these types.
•A keyword is held as a sequence with a first element of -3 (JSON_KEYWORD),and one of the strings "true", "false", or "null".
4. Re: EuMVC:Map to Json
- Posted by ghaberek (admin) Jan 07, 2022
- 866 views
You're right, there should be, but Euphoria isn't typed strongly enough to make a one-to-one translation from a map to JSON, so some type information has to be specified manually.
What I did, at least when parsing json and not that it has anything to do with maps, dunno if this will help:
•Numbers and Strings (without quotes) are held natively
•An object is held as a sequence with a first element of -1 (JSON_OBJECT),and the rest pairs, the first of which is a string and the second any of these types.
•An array is held as a sequence with a first element of -2 (JSON_ARRAY),and the rest any of these types.
•A keyword is held as a sequence with a first element of -3 (JSON_KEYWORD),and one of the strings "true", "false", or "null".
That does help. I may use this approach when I pull the JSON parser over from EuMVC into Euphoria standard library, which I'm hoping to do for 4.2 release. Thanks!
-Greg
5. Re: EuMVC:Map to Json
- Posted by euphoric (admin) Jan 10, 2022
- 754 views
Is there a map_to_json() available in Greg's json lib?
Euphoria isn't typed strongly enough to make a one-to-one translation from a map to JSON, so some type information has to be specified manually.
Ah. OK.
Thank you!