question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Objects not being deserialized from Query

See original GitHub issue

Describe the bug When returning a collection from a query, the items in the collection are default/unpopulated objects.

To Reproduce

        public async Task<IEnumerable<Person>> SearchPersons(string search)
        {
            QueryDefinition qd = await PersonSearchQueryDefinitionParser.BuildSearchQuery(search);
            
            List<Person> ret = new List<Person>();
            
            FeedIterator<Person> feed = Container.GetItemQueryIterator<Person>(qd);
            
            while (feed.HasMoreResults)
            {
                var curr = await feed.ReadNextAsync();

                ret.AddRange(curr);
            }

            return ret;
        }

ret has the expected number of objects in its collection, but they are default. The custom CosmosSerializer is called only once for PartitionedQueryExecutionInfo, but not for either of the returned objects.

Serializer:

public class StorageSerializer : CosmosSerializer 
    {

        private static readonly JsonSerializer Serializer = JsonSerializer.Create(new JsonSerializerSettings {
            Converters = new List<JsonConverter>() { new ContactConverter() },
            ContractResolver = new StorageContractResolver()
        });

        private static readonly Encoding DefaultEncoding = new UTF8Encoding(false, true);

        public override T FromStream<T>(Stream stream)
        {
            using (stream)
            {
                if (typeof(Stream).IsAssignableFrom(typeof(T)))
                {
                    return (T)(object)(stream);
                }

                using (StreamReader sr = new StreamReader(stream))
                using (JsonTextReader r = new JsonTextReader(sr))
                {
                    return Serializer.Deserialize<T>(r);
                }
                
            }
        }

        public override Stream ToStream<T>(T input)
        {
            MemoryStream ret = new MemoryStream();

            using (StreamWriter sw = new StreamWriter(ret, encoding: DefaultEncoding, bufferSize: 1024, leaveOpen: true))
            using (JsonTextWriter jw = new JsonTextWriter(sw))
            {
                jw.Formatting = Formatting.Indented;
                Serializer.Serialize(jw, input);
                jw.Flush();
                sw.Flush();
            }

            ret.Position = 0;

            return ret;
        }
    }

Expected behavior ret should be populated with two deserialized objects.

Actual behavior It appears that deserialization doesn’t happen. Because the Serializer being used is custom and based on Json.NET, the objects aren’t decorated with non-Json.NET attributes.

When debugging, curr.CosmosElements is populated with two objects. The custom CosmosSerializer is not called for the two elements.

Environment summary SDK Version: Microsoft.Azure.Cosmos v3.0.0 OS Version (e.g. Windows, Linux, MacOSX): Windows 10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
davidxcrowecommented, Jul 30, 2019

I can probably come up with something tomorrow.

D

On Tue, 30 Jul 2019 at 19:43, j82w notifications@github.com wrote:

I added a sample https://github.com/Azure/azure-cosmos-dotnet-v3/pull/611 using the custom serializer. Everything appears to be working. Is there any way you can provide a sample that has a repo?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-cosmos-dotnet-v3/issues/583?email_source=notifications&email_token=ABE75EPBUFJ45JHYV5LIDATQCCDUHA5CNFSM4IGC7Z42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3E5OUY#issuecomment-516544339, or mute the thread https://github.com/notifications/unsubscribe-auth/ABE75EMS6IUXRUD7A6CBVADQCCDUHANCNFSM4IGC7Z4Q .

1reaction
davidxcrowecommented, Jul 23, 2019

@j82w

    [JsonObject(MemberSerialization.OptIn, ItemNullValueHandling = NullValueHandling.Ignore)]
    [CADSObject("Person", "/api/persons")]
    public class Person : CADSObject
    {
        public Person() : base("Person")
        { }

        [JsonProperty("personNumber")]
        public virtual int PersonNumber { get; set; }

        [Required]
        [JsonProperty("familyName")]
        public string FamilyName { get; set; }

        [Required]
        [JsonProperty("givenName")]
        public string GivenName { get; set; }

        [Required]
        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("initials")]
        public string Initials { get; set; }

        [Required]
        [JsonProperty("birthDate")]
        public DateTime DateOfBirth { get; set; }

        [JsonProperty("contacts")]
        public IList<Contact> Contacts { get; set; }
        
        [JsonProperty("relatedPersons")]
        public IList<Association> RelatedPersons { get; set; }

        [JsonProperty("associations")]
        public IList<Association> Associations { get; set; }

    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Deserializing JSON object and Query
You are getting an error because your JSON doesnot have a object of array in language as you are expecting in your class...
Read more >
How to deserialize object with nested query in a simply way
I have a JSON of object with nested query and I do not want to use JSON.deserializeUntyped and map values manually. Is there...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). The System.Text.
Read more >
Getting Started with Custom Deserialization in Jackson
This quick tutorial will illustrate how to use Jackson 2 to deserialize JSON using a custom Deserializer.
Read more >
Deserialization - OWASP Cheat Sheet Series
This article is focused on providing clear, actionable guidance for safely deserializing untrusted data in your applications. What is Deserialization¶.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found