Akbar Badhusha
Akbar Badhusha

Reputation: 2627

Json Deserialize error. Unsupported type of : System.Guid

I was working on an API project, and got below issue,

this is the Json I'm passing

{
  "MainClass": [
    {
      "Text": ".",
      "Id": {
        "System.Guid": "06073a9c-9cef-4f07-9180-2e54e0fa6416"
      }
    }
  ]
}

This is my c# Models

public class MainClass {
  public List<SubClass> Subs {get;set;}
}

public class SubClass {
  public string Text {get;set;}
  public Guid Id {get;set;}
}

When i pass the above json i'm getting below error

{ "Message": "The request is invalid.", "ModelState": { "MainClass.SubClass [0].Id": [ "Json Deserialize error. Unsupported type of : System.Guid" ] } }

I have tried below Json also,

{
  "MainClass": [
    {
      "Text": ".",
      "Id": "06073a9c-9cef-4f07-9180-2e54e0fa6416"
    }
  ]
}

How can i fix it?

This happens when i post the json using postman. I can see that it hits the controller, but not the action. It throws error saying as above.

Upvotes: 2

Views: 1392

Answers (3)

ahmeticat
ahmeticat

Reputation: 1939

I usually use json generator like this objects

And so,result is that

public class Id
{
    public string MyGuid { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public Id Id { get; set; }
}

public class RootObject
{
    public List<MainClass> MainClass { get; set; }
}

You can not use System.Guid name as property name.

IMPORTANT : Deserialize object like this;

RootObject myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json.Replace("System.Guid","MyGuid"));

Upvotes: 2

Ketan Kotak
Ketan Kotak

Reputation: 957

you require following class files

public class Id
{
    [JsonProperty("System.Guid")]
    public string System_Guid { get; set; }
}


public class MainClass
{
    public string Text { get; set; }
    public Id Id { get; set; }
}

public class rootClass
{
    public List<MainClass> MainClass { get; set; }
}

Upvotes: 3

er-sho
er-sho

Reputation: 9771

1) For your first json,

{
  "MainClass": [
    {
      "Text": ".",
      "Id": {
        "System.Guid": "06073a9c-9cef-4f07-9180-2e54e0fa6416"
      }
    }
  ]
}

You need to create one more class model to hold the property of type Guid,

public class RootClass
{
    public List<MainClass> MainClass { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public _Guid Id { get; set; }
}

public class _Guid
{
    [JsonProperty("System.Guid")]
    public Guid Id { get; set; }
}

Usage:

RootClass mainClass = JsonConvert.DeserializeObject<RootClass>(json);

mainClass.MainClass.ForEach(x => Console.WriteLine("Text:\t" + x.Text + "\n" + "Id:\t" + x.Id.Id));

Online Demo


2) And for your second json,

{
  "MainClass": [
    {
      "Text": ".",
      "Id": "06073a9c-9cef-4f07-9180-2e54e0fa6416"
    }
  ]
}

You need below class hierarchy to deserialize your json correctly,

public class RootClass
{
    public List<MainClass> MainClass { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public Guid Id { get; set; }       //<= The type "System.Guid" as it is
}

Usage:

RootClass mainClass = JsonConvert.DeserializeObject<RootClass>(json);

mainClass.MainClass.ForEach(x => Console.WriteLine("Text:\t" + x.Text + "\n" + "Id:\t" + x.Id));    

Online Demo


Output: (For both above json)

enter image description here

Upvotes: 2

Related Questions