8

I have a model which looks like that:

class Nested{
  public string Name {get; set;}
  public int Id {get; set;}
}

class Model{
  [JsonProperty]
  public Nested N {get; set;}
  public string Name {get; set;}
  public int Id {get; set;}
}

and a markup for that is something like this:

<input asp-for="Name">
<input asp-for="id">
<input type="hidden" name="n" value="@JsonConvert.SerializeObject(new Nested())">

However when I posting this form back it fails on deserialization because N field looks like encoded twice. So this code works:

var b = JsonConvert.DeserializeAnonymousType(model1, new { N = ""}); 
var c = JsonConvert.DeserializeObject<Nested>(b.N);

but this one fails:

var d = JsonConvert.DeserializeAnonymousType(model1, new {N = new Nested()});

What i need is to make it work with JsonConvert.DeserializeObject<Model>(model1). What should I change to make it work?


example:

{"name":"Abc","id":1,"n":"{\"id\":2,\"name\":\"BBB\"}"}

The same problem described in this question but I'm looking for an elegant, simple solution, which wasn't proposed.

2
  • 3
    JSON serialization question without sample JSON data? Commented Aug 1, 2017 at 11:01
  • Well, I added example, however based on link to similar question and for the situation, that deserialization works in one case and don't work in other - it's clear that json structure not the problem. Well, even problem is already identified - solution the only question. Commented Aug 1, 2017 at 11:39

4 Answers 4

2
+50
class Nested{
  public string Name {get; set;}
  public int Id {get; set;}
}

class Model{
  [JsonProperty]
  public string N {
     get { 
        return JsonConverter.DeserializeObject<Nested>(Nested); 
     } 
     set{
        Nested = JsonConverter.SerializeObject(value);
     }
  }

  // Use this in your code
  [JsonIgnore]
  public Nested Nested {get;set;}

  public string Name {get; set;}
  public int Id {get; set;}
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's nice way to make this work for exact scenario, but the fact that such code will be required in every model with similar structure looks quite disappointed for me.
@silent_coder For single use, this one is easiest, otherwise you can certainly write some interface and JsonDeserializer, and registering it, all is too much work for one time use.
0

I had similar issues but in the opposite direction (due to the EF proxies and that stuff, a long history)

But I'd say that this could be a good hint for you, I did this in my startup, on ConfigureServices method:

// Add framework services.
services.AddMvc().AddJsonOptions(options =>
         {
             // In order to avoid infinite loops when using Include<>() in repository queries
             options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
         }); 

I hope it helps you to solve your issue.

Juan

Comments

0

you can serialize it yourself with your own code, using Runtime.Serialization

something like this

[Serializable]
class Model{
  [JsonProperty]
  public Nested N {get; set;}
  public string Name {get; set;}
  public int Id {get; set;}
 protected Model(SerializationInfo info, StreamingContext context)
        {
            Name = info.GetString("Name");
            Id = info.GetInt32("Id");
            try {
            child = (Model)info.GetValue("N", typeof(Model));
        }

       catch (System.Runtime.Serialization.SerializationException ex)
        {
            // value N not found 
        }

        catch (ArgumentNullException ex)
        {
            // shouldn't reach here, type or name is null 
        }

        catch (InvalidCastException ex)
        {
            // the value N doesn't match object type in this case (Model)
        }
        }
}

once you use Model class as your parameter it will automatically use this serializer we just did.

Comments

-1

try

var d = JsonConvert.DeserializeAnonymousType(model1, new {N = new Nested(), 
        Name = "", Id = 0});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.