Invalid XML Serialization

38 views
Skip to first unread message

Ahmed

unread,
Jul 31, 2013, 4:00:07 PM7/31/13
to halbuil...@googlegroups.com
Hi,

I am using both halbuilder for both HAL_XML & HAL_JSON response types.  For the below Representation, the json-hal generated looks great.  But for XML, the below is what I get, can I get some help on what the problem could be?  As you can see the HaloForm object is not serializing correctly for the XML response.

            Map<String, HaloForm> forms = new HashMap<String, HaloForm>();
            if (device.getForms() != null) {
                for (Form form : device.getForms()) {
                    HaloForm haloForm = new HaloForm();
                    haloForm.setMethod(form.getMethod());
                    haloForm.setTarget(form.getAction());
                    Map <String, HaloField> haloFields = new HashMap <String, HaloField>();
                    for (Field field : form.getFields()) {
                        HaloField haloField = new HaloField();
                        haloField.setOptions(field.getOptions());
                        haloField.setType(field.getFieldType().name());
                        haloFields.put(field.getFieldName(), haloField);
                    }
                    haloForm.setFields(haloFields);
                    forms.put(form.getRel(), haloForm);
                }
                representation.withProperty("_forms", forms);
            }

Correct JSON being generated:

{
    "_forms": {
        "SetPointTemp": {
            "method": "post",
            "target": "myurl/SetPointTemp",
            "fields": {
                "value": {
                    "type": "integer"
                }
            }
        },
        "SetPointPower": {
            "method": "post",
            "target": "rmyUrl/Mode",
            "fields": {
                "value": {
                    "type": "bool"
                }
            }
        }
    }

Invalid XML generation:

<resource href="/my/url/4334082184.0">
  <_forms>
  {SetPointTemp=com.jsonhal.model.HaloForm@1b9b7dfa,
  SetPointPower=com.jsonhal.model.HaloForm@7d6bb63e}
</_forms>
  <properties>{power=false, temp=778}</properties>
</resource>

Mark Derricutt

unread,
Aug 7, 2013, 7:47:46 AM8/7/13
to halbuil...@googlegroups.com
Hi Ahmed,

Sorry I've not replied sooner.

No - you're not doing anything wrong, the codes just... broken. However, that being said - it's broken by design. When I first started HalBuilder my leaning was towards only support SIMPLE key/value pairs, and not arbitrarily nested data structures, mostly as a side effect of wanting to support round-tripping between the XML and JSON variants backwards and forwards.

There are many ways one could serialise nested objects in XML, but not everyone wants to work in the same way.

Currently, the XmlRepresentationWriter simply sets the #TEXT of an element to the .toString() of the value. Support for writing out nested objects was added to JSON awhile ago, and now that the JSON and XML variants have separate release cycles, and I'm not so much concerned with full round-tripping between JSON/XML we should be able to add support for this fairly easily.

The only thing I'm concerned about is how this breaks the interface based rendering/mapping of a representation to a class proxy - altho I'm also wondering if I should even keep support for that in the core library as well.
Reply all
Reply to author
Forward
0 new messages