tags:

views:

260

answers:

4

Im just wondering if I am pushing JSON too far? and if anyone has hit this before?

I have a xml file:

<?xml version="1.0" encoding="UTF-8"?>
<customermodel:Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:customermodel="http://customermodel" xmlns:personal="http://customermodel/personal" id="1" age="1" name="Joe">
<bankAccounts xsi:type="customermodel:BankAccount" accountNo="10" bankName="HSBC" testBoolean="true" testDate="2006-10-23" testDateTime="2006-10-23T22:15:01+08:00" testDecimal="20.2" testTime="22:15:01+08:00">
    <count>0</count>
    <bankAddressLine>HSBC</bankAddressLine>
    <bankAddressLine>London</bankAddressLine>
    <bankAddressLine>31 florence</bankAddressLine>
    <bankAddressLine>Swindon</bankAddressLine>
  </bankAccounts>
</customermodel:Customer>

Which contains elements and attributes....

Which when i convert to JSON gives me:

{"customermodel:Customer":{"id":"1","name":"Joe","age":"1","xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","bankAccounts":{"testDate":"2006-10-23","testDecimal":"20.2","count":"0","testDateTime":"2006-10-23T22:15:01+08:00","bankAddressLine":["HSBC","London","31 florence","Swindon"],"testBoolean":"true","bankName":"HSBC","accountNo":"10","xsi:type":"customermodel:BankAccount","testTime":"22:15:01+08:00"},"xmlns:personal":"http://customermodel/personal","xmlns:customermodel":"http://customermodel"}}

So then i send this too the client.. which coverts to a js object (or whatever) edits some values (the elements) and then sends it back to the server.

So i get the JSON string, and convert this back into XML:

<customermodel:Customer>
    <id>1</id>
    <age>1</age>
    <name>Joe</name>
    <xmlns:xsi>http://www.w3.org/2001/XMLSchema-instance&lt;/xmlns:xsi&gt;
    <bankAccounts>
     <testDate>2006-10-23</testDate>
     <testDecimal>20.2</testDecimal>
     <testDateTime>2006-10-23T22:15:01+08:00</testDateTime>
     <count>0</count>
     <bankAddressLine>HSBC</bankAddressLine>
     <bankAddressLine>London</bankAddressLine>
     <bankAddressLine>31 florence</bankAddressLine>
     <bankAddressLine>Swindon</bankAddressLine>
     <accountNo>10</accountNo>
     <bankName>HSBC</bankName>
     <testBoolean>true</testBoolean>
     <xsi:type>customermodel:BankAccount</xsi:type>
     <testTime>22:15:01+08:00</testTime>
    </bankAccounts>
    <xmlns:personal>http://customermodel/personal&lt;/xmlns:personal&gt;
    <xmlns:customermodel>http://customermodel&lt;/xmlns:customermodel&gt;
</customermodel:Customer>

And there is the problem, is doesn't seem to know the difference between elements/attributes so i can not check against a XSD to check this is now valid?

Is there a solution to this?

I cannot be the first to hit this problem?

+1  A: 

I wouldn't encode the xml schema information in the json string-- that seems a little backwards. If you're going to send them JSON, they shouldn't have any inkling that this is anything but JSON. The extra xml will serve to confuse and make the your interface look "leaky".

You might even consider just using xml and avoid the additional layer of abstraction. JSON makes the most sense when you know at least one party is actually using javascript. If this isn't the case it'll still work as well as any other transport format. But if you already have an xml representation it's a little excessive.

On the other hand, if your customer is really using javascript it will make it easier for them to use the data. The only concern is the return trip, and once it's in JSON who do you trust more to do the conversion back to xml correctly? You're probably better qualified for that, since it's your schema.

Joel Coehoorn
A: 

For this to work you would need to build additional logic/data into your serialize/unserialize methods - probably create something like "attributes" and "data" to hold the different parts:

{"customermodel:Customer":
 { 
   "attributes": {"xmlns:xsi":"...", "xmlns:customermodel":"..."},
   "data":
   {
     "bankAccounts":
     {
       "attributes": { ... }
       "data" : 
       {
         "count":0,
         "bankAddressLine":"..."
       }
     }
   }
 }
jcinacio
I know it not the best, and have pointed this out.. but its needed to tick boxes. I think i might extend the serializer to asd $ to attribute names, then modify the serializer (back to xml) to parse it based on $ = attributes.
joe90
Whatever form you end up with, the things to note are: 1) make sure the identifiers aren't used in the XML, 2) test, test, test! :)
jcinacio
+9  A: 

JSON does not make sense as an XML encoding, no. If you want to be working with and manipulating XML, then work with and manipulate XML.

JSON is for when you need something that's lighter weight, easier to parse, and easier to write and read. It has a fairly simple structure, that is neither better nor worse than XML, just different. It has lists, associations, strings, and numbers, while XML has nested elements, attributes, and entities. While you could encode each one in the other precisely, you have to ask yourself why you're doing that; if you want JSON use JSON, and if you want XML use XML.

Brian Campbell
+1  A: 

JsonML provides a well thought out standard mapping from XML<->JSON. If you use it, you'll get the benefit of ease-of-manipulation you're looking for on the client with no loss of fidelity in elements/attributes.

sblom