Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it Microsoft's Json (System.Runtime.Serialization.Json) bug?

Tags:

json

c#

i'm working with Microsoft Json library. (DataContractJsonSerializer)

i thought it was a nice library but it caught very hard problem for me. first, please see the screenshot please.

enter image description here

the json file was clear. because i test it some online json parse sites like here

a problem is it'cant deserialize json rightly. it was cracked.

i. terms[0].labels=null. but as web parser showed, it contains 'labels' structure. ii. terms1.labels!=null. but as web parser showed, it contains nothing. (terms1 doesn't 'labels' structure, instead terms[0]

a problem is not only that.

the 'primaries' contains number of two array originally, but microsoft json returns just one member only.

even, terms1.labels[0].text have a string value "[US]" but, a string "[US]" is not exist in entire json string!

here is my structure for json.

[DataContract]
public struct GDicJson
{
    [DataMember] public string query;
    [DataMember] public string sourceLanguage;
    [DataMember] public string targetLanguage;
    [DataMember] public List<GDicResultContent> primaries;
    [DataMember] public List<GDicResultContent> webDefinitions;

    [DataContract]
    public struct GDicResultContent
    {
        [DataMember] public string type;
        [DataMember] public List<GDicResultTerm> terms;
        [DataMember] public List<GDicResultEntry> entries;
    }

    [DataContract]
    public struct GDicResultTerm
    {
        [DataMember] public string type;
        [DataMember] public string text;
        [DataMember] public string language;
        [DataMember] public List<GDicResultLabel> labels;
    }

    [DataContract]
    public struct GDicResultLabel
    {
        [DataMember] public string text;
        [DataMember] public string title;
    }

    [DataContract]
    public struct GDicResultEntry
    {
        [DataMember] public string type;
        [DataMember] public List<GDicResultTerm> terms;
        [DataMember] public List<GDicResultEntry> entries;
    }
}

i like to my source json file but StackOverflow doesn't provide upload file. i'm sorry for that. anyway, help me anybody.

if you want, i'll send a json file to your email with my source code.

like image 985
mjk6026 Avatar asked Nov 21 '25 09:11

mjk6026


1 Answers

But still as suggested by @StriplingWarrior, Json.NET has some issues too. I had a correct Json string containing basically just arrays within squared brackets and Json.NET was unable to parse it correctly and was returning nulls. I used fastJSON instead and all problems were solved. Plus is a way faster library than Json.NET. Give it a look at http://www.codeproject.com/KB/IP/fastJSON.aspx it also pretty easy to implement.

like image 197
Sergio A. Avatar answered Nov 22 '25 21:11

Sergio A.