Dynamically generating JSON-like data structures with key/value pairs

83 views
Skip to first unread message

Christopher Johnson

unread,
May 15, 2014, 2:42:48 PM5/15/14
to haxe...@googlegroups.com
I have a function that processes a data structure parsed from JSON.  Internally the function is using Reflect.fields() to scan the data structure.

I'm also creating a second function that returns a data structure that can be directly passed into the first function.

This is working for types like strings and arrays, but is not working for dictionaries.  Map<K, V> is what I've been using in my code and seems like the most natural way to construct such a dictionary, but the output does not match the format of JSON.parse() (It has its "h" property with dollar signs in the keys).

How can I dynamically generate an object filled with key/value pairs that is similar to the output from JSON.parse?  (Is there a clean, efficient way to convert Map to such a format?)

JavaScript is my current target for this code though it will eventually be compiled to other targets as well.

clemos

unread,
May 15, 2014, 2:47:20 PM5/15/14
to haxe...@googlegroups.com
Hi,

The right type for such items is Dynamic<V>, such as Dynamic<String> for a structure like :
{ foo : "test" , bar : "test2" }

Best,
Clément


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Dan Korostelev

unread,
May 15, 2014, 4:04:42 PM5/15/14
to haxe...@googlegroups.com
This topic is brought up so often we should probably make a FAQ section on that. I personally use a wrapper abstract type I made https://gist.github.com/nadako/8370493
I even submitted it as a pull request for Haxe standard library, but got rejected :-)

четверг, 15 мая 2014 г., 22:42:48 UTC+4 пользователь Christopher Johnson написал:

Justin L Mills

unread,
May 15, 2014, 8:18:59 PM5/15/14
to haxe...@googlegroups.com
Dan

people used to put snips here http://haxe.org/doc/snip but not sure if
that is the best place now?

but would be good to have links to haxer gists.

Justin

Dan Korostelev:
Reply all
Reply to author
Forward
0 new messages