0

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; }
    }

1 Answers1

0

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

M M E G
  • 24
  • 4
  • You said you can't comment yet but then you did... Those are the default columns AMAS requires, you can't change them. The service is ignoring all attributes otherwise I wouldn't have posted this question. It does what it wants and I can't change the Json settings. – beatnikthedan Jun 12 '20 at 11:57
  • Yeah I mean I cannot add comments (not to your original post), gotta have 50 points). I can only post answers and then comments to my own answer. Do you have access to the DB to see if its actually stored that way? To my knowledge however, your question is do I use the string or do I use the object, would be the string version. But only based on what you are showing as what you have in the service. What would be interesting is what is in the DB. How did you build the EF model (from the DB directly?)? Did you do Code First from DB? – M M E G Jun 13 '20 at 01:53