Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET serialize object where property name to name that starts with dot

Is it possible to achieve the following:

I have a class:

public class Customer
{
    public Csutomer()
    {
    }

    public string Name { get; set; }
}

and then I instantiate my class:

Customer cust = new Customer();
cust.Name = "Jhon Smith";

string result = JsonConvert.SerializeObject(cust);

and the result will contain:

{"Name":"Jhon Smith"}

What I need is to get the json like this, notice the dot . before Name .Name

{".Name":"Jhon Smith"}

And after that to parse the Json back to my object.

like image 456
David Dury Avatar asked Dec 05 '22 05:12

David Dury


1 Answers

Declare your property as:

[JsonProperty(".Name")]
public string Name { get; set; }
like image 143
L.B Avatar answered Mar 17 '23 02:03

L.B