c# object to json online converter code example

Example 1: json to csharp

//Make use of the third party library called Newton soft.
  
//To serialize a JSON object to C# class. The below will help. 
//Note: The C# class and JSON Object should have the same structure
  public static T Deserialize(string serializedData)
        {
            var deJson = JsonConvert.DeserializeObject<T>(serializedData);
            return deJson;
        }

//To de-serialize a JSON object to C# class. The below will help. 
//Note: The C# class and JSON Object should have the same structure
//The contract resolver and settings are only needed if you have specific attributes in your JSON string
 public static string Serialize(object objectName)
        {
            var settings = new JsonSerializerSettings()
            {
                ContractResolver = new OrderedContractResolver()
            };
            var json = JsonConvert.SerializeObject(objectName, Formatting.Indented, settings);
            return json;

        }
//No settings:
 public static string Serialize(object objectName)
        {
            var json = JsonConvert.SerializeObject(objectName, Formatting.Indented);
            return json;

        }

Example 2: c# object to json online converter

public class ProductChangedEvent : IProductChangedEvent
{
    public OriginEnum Origin { get; set; }

    public long Version { get; set; } = 1L;

    public DateTime CreateDateUTC { get; set; } = DateTime.UtcNow;

    public DateTime OperationDateUtc { get; set; }

    public DateTimeOffset? StartOnlineDate { get; set; }
}

Example 3: c# object to json online converter

public class Root{
    public string schema {get; set;}
    public string name {get; set;}
    public string namespace1 {get; set;}
    public string type {get; set;}
    public Dictionary<String,String> fields{get; set;} 
}

Tags:

Misc Example