Mihail Shishkov
Mihail Shishkov

Reputation: 15797

Change the way JSON.NET serializes property names

How can I change the way Newtonsoft JSON.NET serializes property names of objects?

Upvotes: 2

Views: 3811

Answers (2)

Rayshawn
Rayshawn

Reputation: 2617

You can create a model with the property names. And change them by creating some private variables that will be use to as return values for the properties. This is will direct the deserializer to reset the name of the property.

    private int _privateId;

    public int NameThatExistAlreadyInTheJson 
    {

        set { _privateId = value; }

    }
    public int NameYouWantItToBeDisplayInstead 
    {
        get { return _privateId; }
    }

Upvotes: 2

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107498

A couple of ways:

  1. You can manually control how it serializes using the JsonTextWriter class:
  2. You could implement a custom JsonConverter that does what you want:

Upvotes: 3

Related Questions