Serialize an array of strings

2
I am working on a MailChimp API and to add groups to a Grouping I need to send the groups as a structure of strings {"group1","group2",group3"}. I can not seem to get the JsonSerializer to create anything other than a Json name/value pair object. I looked at the java for the serilaizer and it seems to work with arrays of Json Objects only. MailChimp wants the following json structure { "id": "31dab5452a", "name": "Game driving", "type": "checkboxes", "groups": ["Evening drives", "Daytime drives"], "apikey": "xxxxx" }
asked
3 answers
4

Thanks. I have added additional code to the JsonSerializer module which copes with serializing the primitives based on a subclass of the Primitive entity and it works really well now. I added it for interest

else if (member instanceof MendixObjectReferenceSet){ JSONArray ar = new JSONArray();

        if (value != null) {
            @SuppressWarnings("unchecked")
            List<IMendixIdentifier> ids = (List<IMendixIdentifier>) value;
            for(IMendixIdentifier id : ids) if (id != null) {
                //primitive
                IMendixObject obj = Core.retrieveId(context, id); //Optimize: for refset use retrieve ids
                if (obj != null) {
                    String objType = obj.getType();
                    if (Core.isSubClassOf(Primitive.entityName, objType)) {
                        Primitive prim = Primitive.initialize(context, obj);
                        if (prim.getPrimitiveType() == RestPrimitiveType.String){
                            ar.put(prim.getStringValue(context));
                        }
                        else if(prim.getPrimitiveType() == RestPrimitiveType._Boolean){
                            ar.put(prim.getBooleanValue());
                        }
                        else if(prim.getPrimitiveType() == RestPrimitiveType.Number){
                            ar.put(prim.getNumberValue());
                        }
                        else if(prim.getPrimitiveType() == RestPrimitiveType._NULL){
                            ar.put(JSONObject.NULL);
                        }
                    }
                    else{
                        Object url = identifierToJSON(context, id, alreadySeen);
                        if (url != null)
                            ar.put(url);
                    }
                }
            }
        }
        target.put(Utils.getShortMemberName(memberName), ar);           
    }
answered
1

To serialize plain json values, you can use the JsonPrimitive entity which is already available in the module by default. So if you create a referenceset named 'groups' that points to json primitives, you should be able to generate the desired json structure.

answered
0

Michel, I linked routes to "primitive" in the RestServices Module and set the primitive type to String and got the following Json { "id": "31dab5452a", "groups": [{ "PrimitiveType": "String", "NumberValue": 0, "BooleanValue": false, "StringValue": "Koegel" }], "name": "Birding", "type": "checkboxes", "apikey": "xxxxxx" }

Am a linking to the wrong object ?

answered