I have written a small construct to do that long back, it requires System.Runtime.Serialization.Json
namespace. It uses DataContractJsonSerializer
to serialize & deserialize object
with a static method JConvert
.
It works with small set of data but haven't tested with big data source.
JsonHelper.cs
// Json Serializer without NewtonSoft
public static class JsonHelper
{
public static R JConvert<P,R>(this P t)
{
if(typeof(P) == typeof(string))
{
var return1 = (R)(JsonDeserializer<R>(t as string));
return return1;
}
else
{
var return2 = (JsonSerializer<P>(t));
R result = (R)Convert.ChangeType(return2, typeof(R));
return result;
}
}
private static String JsonSerializer<T>(T t)
{
var stream1 = new MemoryStream();
var ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(stream1, t);
stream1.Position = 0;
var sr = new StreamReader(stream1);
return (sr.ReadToEnd());
}
private static T JsonDeserializer<T>(string jsonString)
{
T deserializedUser = default(T);
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
var ser = new DataContractJsonSerializer(typeof(T));
deserializedUser = (T)ser.ReadObject(ms);// as T;
ms.Close();
return deserializedUser;
}
}
Syntax:
To use the JsonHelper
you need to call
JConvert<string,object>(str); //to Parse string to non anonymous <object>
JConvert<object,string>(obj); //to convert <obj> to string
Example:
Suppose we have a class person
public class person
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
var obj = new person();//"vinod","srivastav");
obj.FirstName = "vinod";
obj.LastName = "srivastav";
To convert the person
object we can call:
var asText = JsonHelper.JConvert<person,string>(obj); //to convert <obj> to string
var asObject = JsonHelper.JConvert<string,person>(asText); //to convert string to non-anonymous object
EDIT:2023
Since the construct & was a bit difficult to use & understand, here is another simple implementation to imitate Javascript JSON
object withJSON.stringify()
& JSON.parse()
. It also have an extension function ToJson()
to work with anonymous objects which uses System.Web.Script.Serialization
from system.Web.Extensions.dll
to serialize object tot json.
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
public static class JSON
{
public static string ToJson(this object dataObject)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(dataObject);
}
public static T ToObject<T>(this string serializedData)
{
JavaScriptSerializer js = new JavaScriptSerializer();
var deserializedResult = js.Deserialize<T>(serializedData);
return deserializedResult;
}
public static String Stringify<T>(T t)
{
var inmemory = new MemoryStream();
var ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(inmemory, t);
return (Encoding.UTF8.GetString(inmemory.ToArray()));
}
public static T Parse<T>(string jsonString)
{
T deserializedUser = default(T);
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
var ser = new DataContractJsonSerializer(typeof(T));
deserializedUser = (T)ser.ReadObject(ms);// as T;
ms.Close();
return deserializedUser;
}
}
Now to use this one you simply have to write:
//to convert <obj> to string
var asText = JSON.Stringify(obj);
//to convert string to non-anonymous object
var asObject = JSON.Parse<person>(asText);
//for anonymous object
var ao = new {Name="Vinod", Surname = "Srivastav" };
var asText = ao.ToJson();
In .NET 6.0
With .NET 6.0
you just need to add
using System.Text.Json;
using System.Text.Json.Serialization;
Json.cs
public static class JSON
{
public static string Stringify<T>(T t)
{
string jsonString = JsonSerializer.Serialize(t);
return jsonString;
}
public static T Parse<T>(string jsonString)
{
T deserializedObject = JsonSerializer.Deserialize<T>(jsonString)!;
return deserializedObject;
}
}
And it still runs the above example:
JSON.NET
and other libraries came about. – IrrelevantJson
inSystem.Web.Helpers
, there'sJsonQueryStringConverter
inSystem.ServiceModel.Web
, there'sJavascriptSerializer
inSystem.Web.Script.Serialization
,DataContractJsonSerializer
inSystem.Runtime.Serialization.Json
... Not at all confusing. – InfrequencyJson.NET
in its ASP.NET Web API. If you thought that wasn't enough, MS is coming up withSystem.Json
but currently is unfit for consumption. And Windows 8 is a special case for MS, so there is alsoJsonValue
inWindows.Data.Json
which is only for Windows 8 and above. – InfrequencySystem.Json
: msdn.microsoft.com/en-us/library/… talks about "preview" and the Nuget package is both (still) labeled "Beta" and has been unlisted, suggesting deprecation. (There is a releasedSystem.Json
, but it is Silverlight-only). – Devereux