[Mono-dev] JSON Deserialization

Antoine Cailliau a.cailliau at ac-graphic.net
Mon Apr 25 03:51:47 EDT 2011


Hi,

Thanks for all your answers.

It still don't work on the git trunk of Mono. Here is the piece of
code. I think, I can report it, with a test case, in the bugzilla.

Out of curiosity, could some display the array of byte produced by "
Encoding.Unicode.GetBytes ("[{\"Name\":\"1\"}, {\"Name\":\"2\"}]")  "
on MS.NET?

Antoine

On 25 April 2011 01:00, Adar Wesley <adar.wesley at gmail.com> wrote:
> Hi,
>
> You need to quote the property name too.  See in the JSON I quoted the Name
> property name as follows: {\"Name\":\"1\"}
> That's what the error message tells you.  It is expecting the " character
> and finding the N of Name.
>
> ---
> Adar Wesley
>
>
> On Sun, Apr 24, 2011 at 11:46 PM, Stifu <stifu at free.fr> wrote:
>>
>> I tried that, and it didn't work here. Maybe I didn't set up something
>> right
>> (which is why self-contained test cases are always welcome).
>> It complains about the fact the token '"' was expected, but 'N' was found.
>>
>> Error message (in French):
>>
>> "System.Runtime.Serialization.SerializationException: Une erreur s'est
>> produite lors de la désérialisation de l'objet de type
>> System.Collections.Generic.IEnumerable`1[[JsonTest.Element, JsonTest,
>> Version=1.0.4131.39037, Culture=neutral, PublicKeyToken=null]]. Jeton '"'
>> attendu, mais 'N' a été trouvé. ---> System.Xml.XmlException: Jeton '"'
>> attendu, mais 'N' a été trouvé.
>>   at Object
>>
>> System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator
>> reader, Boolean verifyObjectName, DataContractResolver
>> dataContractResolver)
>>   at Object
>>
>> System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader
>> reader)
>>   at Object
>>
>> System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream
>> stream)
>>   at static void JsonTest.MainClass.Main(String[] args)"
>>
>> I get the same error message with simple quotes rather than double quotes
>> in
>> the JSON.
>>
>>
>> Adar Wesley wrote:
>> >
>> > Hi,
>> >
>> > In order to make the code work on MS.NET I had to make the following
>> > changes:
>> > 1. mark the class Element with [DataContract] attribute and the Name
>> > property with [DataMember] attribute.
>> > 2. Change the serialized JSON to be with double quotes (") and Name
>> > should
>> > be quoted too: "[{\"Name\":\"1\"}, {\"Name\":\"2\"}]".
>> >
>> > Only after making these changes does it work on MS.NET.
>> >
>> > Hope this helps.
>> >
>> > ---
>> > Adar Wesley
>> >
>> >
>> > On Sun, Apr 24, 2011 at 8:03 PM, Troy Hamilton
>> > &lt;troy.a.hamilton at gmail.com&gt;wrote:
>> >
>> >>
>> >> On Sun, Apr 24, 2011 at 8:53 AM, Antoine Cailliau <
>> >> antoinecailliau at gmail.com> wrote:
>> >>
>> >>> Hi folks,
>> >>>
>> >>>
>> >>>  This piece of code fails to work on Mono.
>> >>>
>> >>
>> >>> ("[{Name:'1'}, {Name:'2'}]"));
>> >>>
>> >>>
>> >>> The raised exception is
>> >>>
>> >>>        Unhandled Exception:
>> >>> System.Runtime.Serialization.SerializationException: Deserialization
>> >>> has failed ---> System.Xml.XmlException: Unexpected token: '
>> >>>
>> >>>
>> >> Can you try replacing the single quotes with escaped double quotes and
>> >> see
>> >> what you get?
>> >>
>> >> "[{Name:\"1\"}, {Name:\"2\"}]"
>> >>
>> >> I've run into a json libraries before that deal with single quotes
>> >> allright, but I believe the standard is to use double quotes.
>> >>
>> >>
>> >> Good luck,
>> >>
>> >> Troy
>> >>
>> >>
>> >>
>> >> _______________________________________________
>> >> Mono-devel-list mailing list
>> >> Mono-devel-list at lists.ximian.com
>> >> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>> >>
>> >>
>> >
>> > _______________________________________________
>> > Mono-devel-list mailing list
>> > Mono-devel-list at lists.ximian.com
>> > http://lists.ximian.com/mailman/listinfo/mono-devel-list
>> >
>>
>>
>> --
>> View this message in context:
>> http://mono.1490590.n4.nabble.com/JSON-Deserialization-tp3471570p3472057.html
>> Sent from the Mono - Dev mailing list archive at Nabble.com.
>> _______________________________________________
>> Mono-devel-list mailing list
>> Mono-devel-list at lists.ximian.com
>> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>


More information about the Mono-devel-list mailing list