I'm porting a project from Android to Windows Phone 8 and can't find any info on how to specify which JSON key maps to which object field.
The Android code uses Google GSON and the SerializedName annotation to map em in the JSON to the email field in the object.
I'm also controlling which fields are [de]serialized using the Expose annotation. How would I go about doing these same things in my Windows Phone 8 project?
I'd absolutely hate to use a class that looks like this:
public sealed class SomeData
{
public string em { get; set; }
public string un { get; set; }
public string fn { get; set; }
public int tz { get; set; }
}
Thanks.
You can mark properties with DataMember attribute to explicitly specify desired name.
[DataMember(Name = "em")]
public string Email { get; set; }
Related
I'm trying to store a model to the redis that has 3 string fields, each one is serialized JSON
I have no problem in setting it and I can see it in redis insight
but when getting it from redis it fires error JSON exception when digging deep into it, the problem with those string fields that contains JSON
[Document(Prefixes = new[] { "Demo" })]
public class Demo
{
[Indexed]
public string ReferenceNumber { get; set; }
[Indexed]
public string Note { get; set; }
[Indexed]
public string ItemsData { set; get; } <-- those the string fields that contains json
[Indexed]
public string SkillsData { get; set; }
[Indexed]
public string Data { get; set; }
}
I'm using redis OM with Dot Net Core 6.
Furthermore, I tried to change the document type from hash to JSON and nothing happened
I didn't find a way to do so in HashSet so I decided to change the type of the document to Json and I got to retrieve the data without any problem
I am trying to implement the Syncfusion Blazor QueryBuilder component to build dynamic search filters.
I can successfully store query builder rules to my DB after mapping to my C# class model.
But when I try to re-map these rules back to the Syncfusion "RuleModel" class I get error below in the browser.
It appears to be caused by the dynamic property types on the "Operate" and "Value" fields.
When I get the error, these properties have the "ValueKind" element. When this is not present, it works fine (eg. If I manually create a new RuleModel())
Error in Browser, when QueryBuilderObj.SetRules() method is called..
Error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'System.Text.Json.JsonElement' to 'string'
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at Syncfusion.Blazor.QueryBuilder.Internal.QueryBuilderRules`1.SetField()
at Syncfusion.Blazor.QueryBuilder.Internal.QueryBuilderRules`1.OnParametersSetAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
Client Code
<SfQueryBuilder TValue="#FilterColumns" #ref="QueryBuilderObj" MaxGroupCount=3>
<QueryBuilderColumns>
<QueryBuilderColumn Field="Status" Label="Status" Type="ColumnType.String"></QueryBuilderColumn>
<QueryBuilderColumn Field="DepartmentCode" Label="DepartmentCode" Type="ColumnType.String"></QueryBuilderColumn>
</QueryBuilderColumns>
</SfQueryBuilder>
<button type="button" #onclick="getRules">Get Rules</button>
#code {
SfQueryBuilder<FilterColumns> QueryBuilderObj;
public class FilterColumns
{
public string Status { get; set; }
public string DepartmentCode { get; set; }
}
[Parameter]
public RuleModel rules { get; set; }
private void getRules()
{
QueryBuilderObj.SetRules(rules.Rules, rules.Condition);
}
}
Syncfusion RuleModel Class
public class RuleModel
{
public RuleModel();
public string Condition { get; set; }
public string Field { get; set; }
public string Label { get; set; }
public bool? Not { get; set; }
public dynamic Operator { get; set; }
public string Type { get; set; }
public dynamic Value { get; set; }
public List<RuleModel> Rules { get; set; }
}
I have replicated the syncfusion RuleModel class exactly as above in my domain model.
Has anyone successfully stored and retrieved QueryBuilder rules from Blazor UI into a C# model/class?
Thanks.
I figured out this problem is due to System.Text.Json behaviour causing the newly created property to have a ValueKind object on each property.
Basically this problem... How do I get System.Text.Json to deserialize objects into their original type?
When I switch to using NewtonSoft JSON.NET to de-serialize the RuleModel, it solved the issue.
Not sure how to make it work with System.Text.Json - but will use Json.Net for now.
I'm having a similar problem as posted here
Except I'm using an Azure Mobile App Service Backend.
I have json string stored in the sql database and I need the App Service to de-serialize it to the object type.
Currently the Json from the Azure Mobile App Service Get looks like this:
(and this isn't the in-code formatting, it's actually formatting the json that way)
{"deleted": false,"updatedAt": "2020-06-09T16:30:48.09Z","createdAt": "2020-06-03T04:34:41.617Z","version": "AAAAAABXrYA=","id": "DBEC6DE9-3C5C-47C5-8404-67C79FCF6740","equipmentSpeakerTapValue": "{\"value\": 0.0,\"wattage\": 0,\"direct\": false}"}
equipmentSpeakerTapValue is shown as a string an not a json nested object
I need the Json to look like this:
{"deleted": false,"updatedAt": "2020-06-09T16:30:48.09Z","createdAt": "2020-06-03T04:34:41.617Z","version": "AAAAAABXrYA=","id": "DBEC6DE9-3C5C-47C5-8404-67C79FCF6740","equipmentSpeakerTapValue":{"Value":2.5,"Wattage":70,"Direct":false}"}
Since I'm using Azure Mobile App Service I don't know if the problem is with the Entity Framework or the Json Serializing/DeSerializing. I also don't know how to change it since Azure Mobile App Service is a wrapper so you can't do things normally.
Here is my EF object model and the model of what I need the string property to deserialize to:
public class SiteEquipment
{
public string Id { get; set; }
public byte[] Version { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public bool Deleted { get; set; }
//I don't know which EquipmentSpeakerTapValue to use:
//string version that serializes to: "equipmentSpeakerTapValue": "{\"value\": 0.0,\"wattage\": 0,\"direct\": false}"
public string EquipmentSpeakerTapValue { get; set; }
//object version that should serialize to: "equipmentSpeakerTapValue":{"Value":2.5,"Wattage":70,"Direct":false}"
public TapValue EquipmentSpeakerTapValue { get; set; }
}
public class TapValue
{
public double Value { get; set; }
public int Wattage { get; set; }
public bool Direct { get; set; }
}
sorry I can't comment yet.
I noticed in your second string you only capitalized the last 3 "items" in the json. Do you not care if the previous are capped?
Your String Above
{"deleted": false,"updatedAt": "2020-06-09T16:30:48.09Z","createdAt": "2020-06- 03T04:34:41.617Z","version": "AAAAAABXrYA=","Id": "DBEC6DE9-3C5C-47C5-8404-67C79FCF6740","equipmentSpeakerTapValue":{"Value":2.5,"Wattage":70,"Direct":false}"}
Verification Question String
{"Deleted": false,"UpdatedAt": "2020-06-09T16:30:48.09Z","CreatedAt": "2020-06-03T04:34:41.617Z","Version": "AAAAAABXrYA=","id": "DBEC6DE9-3C5C-47C5-8404-67C79FCF6740","EquipmentSpeakerTapValue":{"Value":2.5,"Wattage":70,"Direct":false}"}
I am actually surprised your first values, such as updatedAt get deserialized properly.
What I would do is in your class definitions, add the JsonProperty attribute so that json knows to deserialize the non-backed parameter (such as Wattage doesn't have a private wattage property)
So put
public class TapValue
{
[JsonProperty("value")]
public double Value { get; set; }
[JsonProperty("wattage")]
public int Wattage { get; set; }
[JsonProperty("direct")]
public bool Direct { get; set; }
}
Are you using Newtonsoft? Because the above would tell it to map the lower case versions to the Upper case versions. Or if you had MyValueHere, you might have myValueHere in json, so make that your jsonproperty value myValueHere and it will map to MyValueHere
Having just merged two branches of a class library, I find one has added several JsonIgnore attributes to model properties. These will break the web client of that library, but are required in the mobile client of that library. I would like to create a new 'Json ignore' attribute that will only ignore marked properties if the calling code is not the web client. Then I would like to hook into the serialization code to look at my new attribute vs. the normal one.
Not a good idea. My suggestion is the classes(let's call them models) in the shared project are really shared (with no attributes), and in other projects where the models are outputted you define classes with attributes. Something like this:
//in MyProject.Core which is shared
class User
{
public int Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
//in MyProject.WcfApi which has wcf services for other teams
[DataContract]
class UserOutput
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string UserName { get; set; }
//no password property here
}
//in MyProject.WebApi which has some web apis for js
class UserOutput
{
[MyJsonRelatedAttribute]
public int Id { get; set; }
}
Im starting a new WP7 app that gets information from a DNLA server. The server supports JSON languages for the REST protocol. I did some research and found json.net was very well recommended.
I can get data by using http://192.168.1.1:234/rest/status?media=json
Giving me:
{"serverStatus":"STARTED","renderers":[{"uuid":"00a0965fa15c","ipAddress":"192.168.1.10","name":"KDL-52NX803","profileId":"9","status":"ACTIVE"},{"uuid":"1829220b083f","ipAddress":"192.168.1.13","name":"Windows Media Player","profileId":"1","status":"INACTIVE"},{"uuid":"5d70ac53cf8e","ipAddress":"192.168.1.14","name":"Unrecognized device","profileId":"1","status":"UNKNOWN"},{"uuid":"60465a95eec4","ipAddress":"192.168.1.22","name":"Playstation 3","profileId":"4","status":"UNKNOWN"},{"uuid":"001dd860bce4","ipAddress":"192.168.1.9","name":"Xbox 360","profileId":"3","status":"INACTIVE"}]}
Im very much new to c#, ive read the official Json.NET documenation, but would prefer to see sample code to get me moving.
I have created listbox to collect "renderers" data, and TextBlock for the current "serverStatus" of the server.
If anyone can help and would very much appreciate your efforts
For starters, http://json2csharp.com/ is handy for putting in your json and creating a poco.
public class Renderer
{
public string uuid { get; set; }
public string ipAddress { get; set; }
public string name { get; set; }
public string profileId { get; set; }
public string status { get; set; }
}
public class RootObject
{
public string serverStatus { get; set; }
public Renderer[] renderers { get; set; }
}
This page has simple examples on serialize/dserialize.
RestSharp is a wonderful HTTP API Client that supports WP7. It will deserialzie for you and also allows you to implement your own serializer/deserializer.