Linq not recognizing JsonPropertyName when using System.Text.Json

  • Thread starter Thread starter timmyv123
  • Start date Start date
T

timmyv123

Guest
Hi,

When converting from Newtonsoft.Json to System.Text.Json I have encountered problems when using Linq queries. Json is case sensitive and because the column names in Cosmos DB begin with lower case letters, I would have set the Json Property name in the class to match that but have the actual property name begin with an upper class letter to align with naming conventions best practice. For example:



public class QuarterlyBonusCosmos : QuarterlyBonusDetail
{
[JsonProperty("logonidLower")]
public string LogonidLower { get; set; }



[JsonProperty("yearQuarter")]
public int YearQuarter { get; set; }
}



The equivalent for this JsonProperty attribute is JsonPropertyName attribute in System.Text.Json. So an example property in System.Text.Json would be:



[JsonPropertyName("id")]

public string Id { get; set; }



[JsonPropertyName("idLower")]
public string IdLower { get; set; }



The problem I am encountering is that my LINQ queries aren't using the JsonPropertyName attribute whereas they would have used the JsonProperty attribute when I was using Newtonsoft.Json.



For example:



var query = client.CreateDocumentQuery<RankBoardOutputv3>(collectionUri, new FeedOptions { EnableCrossPartitionQuery = true })

.Where(d => d.idLower == idLower)
.Where(d => d.isGrossAP == data.IsGrossAP.ToLower())
.Where(d => d.isAnnualSalesQuota == data.IsAnnualSalesQuota.ToLower())
.ToList();



This query does not return any resuklts when the properties begin with an Upper case letter and the JsonPropertyName begins with lower case such as the ID & Idlower properties shown above. Whereas when I was using Newtonsoft.Json, the LINQ query would have correctly serialised the JsonProperty and used the name in there rather than the actual property name. Now, to get the code to work I have to set the actual property names to match e.g.



public string idLower {get; set;} which is bad practice as properties are meant to begin with an uppercase letter.



In other words, the actual property names need to be identical to the column names in the cosmos database for it to return output, it doesn't register the JsonPropertyName attributes or at least it doesn't appear to.



One last point is that if i create a test RankBoardOutputv3 (the type used within the linq query) and i debug it and view the output file, it is serialized correctly using the JsonPropertyNames so the problem seems specific to LINQ.


Can someone assist me with this query?

Continue reading...
 
Back
Top