Skip to content

Deserialize particular object #141

Closed

Description

@protyagov

I am trying to deserialize a particular object from stream. Object looks like this:

{
"Tracker_Name":"xxxx x",
"Tracker_SearchTerm":"Wind Power",
"Tracker_SortBy":"Relevance"
}

Deserialize it like that:

using (TextReader reader = new StreamReader(stem)) 
    return NetJSON.NetJSON.Deserialize<T>(reader);

into the following object

public class Tracker {
        [NetJSONProperty("Tracker_Name")]
        public string Name { get; set; }

        [NetJSONProperty("Tracker_SearchTerm")]
        public string SearchTerm { get; set; }

        [NetJSONProperty("Tracker_SortBy")]
        public string SortBy { get; set; }
}

I get not null object where all properties are set to null. The strange thing is that it somehow reacts to value "xxxx x". If I have there instead "xxxxx" or "xxx xx" it works just fine. For some reason only certain combinations yield error. If JSON string has "Tracker_Name" as last param then first 2 params get correct value and name still be null.
If JSON string has space before " :" then deserializing works fine for any value.

The same manipulation with "Tracker_SearchTerm" param appear to have no affect on result. For some reason only Name property does that...

Not sure why this happens. Perhaps I need to setup something extra but I am not sure what...

Activity

  1. rpgmaker commented on Dec 9, 2016

    @rpgmaker
    Owner

    Thanks for the find. I will take a look at it over the weekend and try to come up with a resolution.

    Thanks,

  2. self-assigned this
    on Dec 11, 2016
  3. rpgmaker commented on Dec 11, 2016

    @rpgmaker
    Owner

    I tried to reproduce it in multiple ways and I can't. I tried using a text file (load using stream), used a json string with both values that you said is invalid and it always works. What version are you using?

    var json = @"{
    ""Tracker_Name"":""xxxx x"",
    ""Tracker_SearchTerm"":""Wind Power"",
    ""Tracker_SortBy"":""Relevance""
    }";
     var json2 = @"{
    ""Tracker_Name"":""xxxx x x"",
    ""Tracker_SearchTerm"":""Wind Power"",
    ""Tracker_SortBy"":""Relevance""
    }";
                
                var tracker = NetJSON.Deserialize<Tracker>(json);
                var tracker2 = NetJSON.Deserialize<Tracker>(json2);
                Tracker tracker3 = null;
    
                using (TextReader reader = new StreamReader(File.OpenRead(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "trackerjson.txt"))))
                {
                    tracker3 = NetJSON.Deserialize<Tracker>(reader);
                }
    
                Assert.IsTrue(tracker.Name == "xxxx x", tracker.Name);
                Assert.IsTrue(tracker.SearchTerm == "Wind Power", tracker.SearchTerm);
                Assert.IsTrue(tracker.SortBy == "Relevance", tracker.SortBy);
    
                Assert.IsTrue(tracker2.Name == "xxxx x x", tracker2.Name);
                Assert.IsTrue(tracker2.SearchTerm == "Wind Power", tracker2.SearchTerm);
                Assert.IsTrue(tracker2.SortBy == "Relevance", tracker2.SortBy);
    
                Assert.IsTrue(tracker3.Name == "xxxx x", tracker3.Name);
                Assert.IsTrue(tracker3.SearchTerm == "Wind Power", tracker3.SearchTerm);
                Assert.IsTrue(tracker3.SortBy == "Relevance", tracker3.SortBy);
    
  4. protyagov commented on Dec 13, 2016

    @protyagov
    Author

    Looking at you sample data it is hard to say if you have a spaces around ":". After posting this I was looking for temp workaround for us as I want to keep using your library. Here is what I did for work around:

            public static T fromStream<T>(Stream strm) {
                using (TextReader reader = new StreamReader(strm)) {
                    string text = reader.ReadToEnd().Replace("\":", "\" :");               
                    T t = NetJSON.NetJSON.Deserialize<T>(text);
                    return t;
                    }
                }
    

    I am using version 1.2.1.5 from NuGen

  5. rpgmaker commented on Dec 13, 2016

    @rpgmaker
    Owner

    Funny I am not getting same issue with same code.

  6. rpgmaker commented on Dec 13, 2016

    @rpgmaker
    Owner

    It might be an encoding issue. Could you recreate the issue in a sample project and upload it here?

    Thanks,

  7. protyagov commented on Dec 13, 2016

    @protyagov
    Author

    Yes, strange thing.
    Will try to provide you with something as soon as I can.

  8. rpgmaker commented on Dec 13, 2016

    @rpgmaker
    Owner

    In my sample data I am using same values you provided I am not including any spaces.

  9. protyagov commented on Dec 13, 2016

    @protyagov
    Author

    Just posted a quick video with real example on what I am doing:
    https://youtu.be/F8AV86CWRyw

    Not sure when I can make a sample project. Perhaps this might help.
    The following does not get deserialized correctly:

            public static T fromStream<T>(Stream strm) {
                using (TextReader reader = new StreamReader(strm)) {
                    string text = "{\n\"Tracker_SortBy\":\"Relevance\",\n\"Tracker_Name\":\"Wind Power Org\",\n\"Profile_Tracker_ID\":1428,\n\"Tracker_ContentType\":\"1\",\n\"Tracker_SearchTerm\":\"Wind Power\",\n\"Tracker_Facets\":[\n{\"Tracker_Facet\":\"{fb8d09e1-5024-419e-9703-598945af8139}\"},\n{\"Tracker_Facet\":\"{90ec93d4-e1bd-4215-acf0-eac1e2fd5f6d}\"}]\n}";
                    Tracker t = NetJSON.NetJSON.Deserialize<Tracker>(text);
                    return default(T);
                    }
                }
    
    

    Tracker Class:

        public class Tracker {
            [NetJSONProperty("Tracker_Name")]
            public string Name { get; set; }
    
            [NetJSONProperty("Profile_Tracker_ID")]
            public int ID { get; set; }
    
            [NetJSONProperty("Tracker_ContentType")]
            public string ContentType { get; set; }
    
            [NetJSONProperty("Tracker_SearchTerm")]
            public string SearchTerm { get; set; }
    
            [NetJSONProperty("Tracker_SortBy")]
            public string SortBy { get; set; }
    
            [NetJSONProperty("Tracker_Facets")]
            public System.Collections.Generic.List<Facet> FacetCollection { get; set; }
            }
        }
    
    

    Facets class

        public class Facet {
            public Facet() { }
    
            public Facet(string _facet) {
                //Value = _facet;
                }
    
            [NetJSONProperty("Profile_Tracker_Facets_ID")]
            public int ID { get; set; }
    
            [NetJSONProperty("Profile_Tracker_ID")]
            public int TrackerID { get; set; }
    
            [NetJSONProperty("Tracker_Facet")]
            public Guid Value { get; set; }
            }
    
  10. NimaAra commented on Dec 13, 2016

    @NimaAra
    Contributor

    It seems the issue is related to the ID property.

    Given a model:

    public class Tracker
    {
        [NetJSONProperty("Tracker_Name")]
        public string Name { get; set; }
    
        [NetJSONProperty("Profile_Tracker_ID")]
        public int ID { get; set; }
    
        [NetJSONProperty("Tracker_ContentType")]
        public string ContentType { get; set; }
    
        [NetJSONProperty("Tracker_SearchTerm")]
        public string SearchTerm { get; set; }
    
        [NetJSONProperty("Tracker_SortBy")]
        public string SortBy { get; set; }    
    }
    

    The following returns null for all properties:

    void Main()
    {
        var input = @"{
      ""Tracker_Name"":""xxxx x"",
      ""Tracker_SearchTerm"":""Wind Power"",
      ""Tracker_SortBy"":""Relevance""
    }";
        
        NetJSON.NetJSON.Deserialize<Tracker>(input).Dump();
    }
    

    Now if you delete the ID property everything is deserialized correctly.

    On another interesting issue, if you change the input to:

    void Main()
    {
        var input = @"{
          ""Tracker_Name"":""xxxx x"",
          ""Tracker_SearchTerm"":""Wind Power"",
          ""Tracker_SortBy"":""Relevance""
        }";
        
        NetJSON.NetJSON.Deserialize<Tracker>(input).Dump();
    }
    

    Note it is the same input I have just inserted a tab at the beginning of each line, now the program goes into a never ending loop :-(

    @rpgmaker, I remember I opened an issue related to this and as far as I remember you did actually fix it but here it is manifesting again.

  11. rpgmaker commented on Dec 13, 2016

    @rpgmaker
    Owner

    The issue with it looping forever is the UseStringOptimization option, setting it to false fixes the problem. I will see what I can do to fix it.

    Thanks,

  12. rpgmaker commented on Dec 14, 2016

    @rpgmaker
    Owner

    The issue has been resolved and pushed to nuget also
    https://www.nuget.org/packages/NetJSON/

    I hope it did not broke anything because all my unit test pass including the new two unit test i wrote

  13. rpgmaker commented on Dec 14, 2016

    @rpgmaker
    Owner

    The issue with it also reading the result and getting null for some is due to the effect of UseStringOptimization which tries to avoid doing character by character reading of json string to improve performance.

  14. protyagov commented on Dec 14, 2016

    @protyagov
    Author

    Thank you for quick fix. Switched project to 1.6 and everything now appear to work correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions