IBMStreams / streamsx.json

Toolkit for working with JSON in SPL applications.
http://ibmstreams.github.io/streamsx.json/
Other
3 stars 19 forks source link

JsonToTuple should be able to process maps #117

Closed saberem closed 5 years ago

saberem commented 5 years ago

Hi, in https://github.com/IBMStreams/streamsx.json/blob/842b8ee1c35cd438bc047c69b881d276234d8d45/com.ibm.streamsx.json/impl/java/src/com/ibm/streamsx/json/converters/JSONToTupleConverter.java#L180 it's specified that the maps are not supported. I'm wondering what's the reason for the unsupported types and with the current version is it possible to add support for them, especially the map types which is very common?

Thanks!

Cheng

saberem commented 5 years ago

One method that I use to patch the toolkit for the function is here. I use release 1.4.6 as the base version as we are still on 4.2 and does not support Optional type. I haven't tested it for all map use cases but it works for my project.

case MAP:
    Map<Object, Object> jMap = new HashMap();
    MapType mType = (MapType)type;
    Type keyType = mType.getKeyType();
    Type valueType = mType.getValueType();
    JSONObject jo = (JSONObject)jsonObj;
    for(Map.Entry<Object, Object> e : (Set<Map.Entry<Object, Object>>)jo.entrySet())
    {
        jMap.put(jsonToAttribute(name, keyType,e.getKey(),mType), jsonToAttribute(name, valueType, e.getValue(), mType));
    }
    return jMap;
leongor commented 5 years ago

You can try extractFromJSON native function instead: https://github.com/IBMStreams/streamsx.json/blob/846e6804fd084010719b14c3d76145a96bf56a8a/com.ibm.streamsx.json/com.ibm.streamsx.json/native.function/function.xml#L66

Here the sample: https://github.com/IBMStreams/streamsx.json/blob/cf8864e612529e0fa8dd63282cd2b250e6aca34c/samples/JSONToSPLConvert/com.ibm.streamsx.json.sample.jtos/ExtractFromJSON.spl

saberem commented 5 years ago

@leongor , Thanks for sharing the extractFromJson method! It's useful when we want to extract the map key as attribute out of the json map. I'm not sure how I can use it for our use case though, because our goal is not to extract the map key as attribute, but to put the whole map in a map type attribute. I'm not familiar with native cpp functions, but after reading this https://github.com/IBMStreams/streamsx.json/blob/846e6804fd084010719b14c3d76145a96bf56a8a/com.ibm.streamsx.json/impl/include/JsonReader.h#L449 I think we cannot use the existing function to get what we want.

leongor commented 5 years ago

@ASCYang Actually I tested it with map before suggesting :) It takes the whole map to the SPL map attribute.

saberem commented 5 years ago

@leongor I tested it and it worked for us! I will use this function instead of the JsonToTuple operator to do the job. Thanks for the suggestion!