3

is it somehow possible to generate an empty object from an XML schema with JSONIX? I generally have problems creating new JS objects that fit to the XML schema. Thus, this would be really helpful. Any example would be very appreciated. I tried the following to create a new object. NodeType is a complex type name in this case.

{name: {localpart: nodeType}, value:{}};

Then I tried to fill values (I traverse through the schema mappings to find out all possible properties for each type). However, I get e.g. the following error that does not help me very much: Element [ELEMNAME] is not known in this context

If this is not possible, how do I in general create a new object that is supposed to be conform to the schema?

Thanks a lot for any idea!

EDIT: Okay to be more specific here an example:

"NodeType":{
        "type":"object",
        "title":"NodeType",
        "properties":{
            "id":{
                "title":"id",
                "allOf":[
                    {
                        "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
                    }
                ],
                "propertyType":"attribute",
                "attributeName":{
                    "localPart":"id",
                    "namespaceURI":""
                }
            },
            "x":{
                "title":"x",
                "allOf":[
                    {
                        "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
                    }
                ],
                "propertyType":"attribute",
                "attributeName":{
                    "localPart":"x",
                    "namespaceURI":""
                }
            },
            "y":{
                "title":"y",
                "allOf":[
                    {
                        "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
                    }
                ],
                "propertyType":"attribute",
                "attributeName":{
                    "localPart":"y",
                    "namespaceURI":""
                }
            }

This is one JSON Schema excerpt from my XSD file. What I want is an Object that looks like this:

{id:"", x: "", y: ""}

The goal is to marshall this object then to XML.

mapf
  • 496
  • 1
  • 5
  • 21

2 Answers2

2

Disclaimer: I'm the author of Jsonix.

If I got your question right, you're asking, how could you construct a JS object that would be marshallable with Jsonix then.

The trick most people do is to take an XML "this is how it should look like", unmarshal it and the log JSON.stringify(myObject, null 2), something like that. Then you'll see how the appropriate JS should look like.

What you could also do is to generate an JSON Schema out of your XML Schema. This will get you a JSON Schema like this one, providing the complete description of the JSON you need for marshalling.

I also consider implementing something like generation of the TypeScript classes, but that's future music.

Update

I'm sorry, I'm still not quite sure what your question is. You've added an JSON Schema which defines a NodeType complex type with a string id and decimal x and y properties.

You're asking what an empty object for this would be. Well, an empty object for your complex type would be just {}.
An object with values would be for instance {id:"someId", x: 4, y:2}.

But in XML you can't marshal just a complex type, you marshal an element of complex type. Which is represented in a form of a { name: ..., value: ... }. So you'd probably have something like:

{
  "name": {
    "namespaceURI": "uri",
    "localPart": "root"
  },
  "value": {
    "id" : "someId",
    "x" : 4,
    "y" : 2,
    "TYPE_NAME": "NodeType"
  }
}

Hope this helps.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks a lot for your support! Does this mean I have to create an empty XML object of every complex type? I will try both approaches, in fact I already generated a JSON schema. However I do not know for how I can create an empty object from it automatically which is needed since I have a very big schema with multiple files. – mapf Jun 30 '15 at 16:56
  • @mapf Now I'm not quite sure what you mean by "empty XML object" and why you'd have to create it per complex type. `{}` is an empty object. If you just need to know how your JSON should look like, either unmarshal or read the generated JSON Schema or Jsonix mappings. With TypeScript you'd have autocomplete in IDE but that's future music. Ask [here](https://github.com/highsource/jsonix/issues) if you need more thorough explanation. – lexicore Jun 30 '15 at 18:41
  • I added an example to my start post. Maybe now it is a little bit more clear. – mapf Jul 02 '15 at 16:46
  • @mapf Sorry, not much. But I've tried to give more info, please see my update. – lexicore Jul 05 '15 at 15:28
  • Thanks a lot, your update helped me! I was not aware of the structure I needed to take care of! – mapf Jul 12 '15 at 14:10
1

To clarify this issue a bit more: Javascript is a loosely typed language. Even though a JS class has a prototype structure, a JS object has that structure only when its members are populated with values. So Jsonix generating JS mappings from XSD and then the JS script instantiating a new JS object from the mappings doesn't create an unpopulated structure. Only when the object is populated with values is the structure created. That population can be done directly in code by assigning values to the new object, drilling down its tree of contained members populating each one after that member is added to the complex overall JS object, the programmer using the XSD or its docs as a guide. Or the complex overall JS object can be populated by a JSON block, in which case the JSON can be generated from XML of the complex object unmarshaled by Jsonix. The unmarshaling (and any marshaling) can be executed by Jsonix. The bottom line is that either existing XML of the complex object to be un/marshaled, or line by line assignments in the implicit structure must be supplied to the code that uses Jsonix for its features. It's not like in Java (eg. JAXB) where an XSD can be used to generate a new complex object with all its structure that is then populated by the Java program (by assignments in the tree or by unmarshaling from XML).

Matthew
  • 757
  • 11
  • 19