[DataMember] internalint age; } classProgram { staticvoidMain(string[] args) { var p = new Person(); p.name = "John"; p.age = 42; var memoryStream = new MemoryStream(); var ser = new DataContractJsonSerializer(typeof(Person)); ser.WriteObject(memoryStream, p); memoryStream.Position = 0; var sr = new StreamReader(memoryStream); Console.WriteLine(sr.ReadToEnd()); memoryStream.Position = 0; Person p1 = (Person)ser.ReadObject(memoryStream); Console.WriteLine(p1.name); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Security.Principal; using System.Text.RegularExpressions;
namespaceDataContractJsonDeserializer { [DataContract] internalclassPerson { [DataMember] internalstring name; [DataMember] internalint age; [DataMember] object o; } classProgram { staticvoidMain(string[] args) { var settings = new DataContractJsonSerializerSettings(); settings.EmitTypeInformation = EmitTypeInformation.Always; var ser = new DataContractJsonSerializer(typeof(Person), settings); using (FileStream file = new FileStream("1.json", FileMode.OpenOrCreate)) { Person person = new Person(); person.name = "jack"; person.age = 19; ser.WriteObject(file, person); } Console.ReadKey(); } } }
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json;
namespaceDataContractJsonDeserializer { classProgram { staticvoidMain(string[] args) { var settings = new DataContractJsonSerializerSettings(); settings.EmitTypeInformation = EmitTypeInformation.Always; var ser = new DataContractJsonSerializer(Type.GetType("System.Security.Principal.WindowsPrincipal"), settings);
using (FileStream file = new FileStream("1.json", FileMode.OpenOrCreate)) { ser.ReadObject(file); } Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Security.Principal;
namespaceDataContractJsonDeserializer { [DataContract] internalclassPerson { [DataMember] internalstring name; [DataMember] internalint age; [DataMember] object o; } classProgram { staticvoidMain(string[] args) { var settings = new DataContractJsonSerializerSettings(); settings.EmitTypeInformation = EmitTypeInformation.Always; var ser = new DataContractJsonSerializer(typeof(Person), new List<Type> { typeof(WindowsPrincipal) }); using (FileStream file = new FileStream("1.json", FileMode.OpenOrCreate)) { ser.ReadObject(file); } Console.ReadKey(); } } }
using System; using System.CodeDom; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Xml.Serialization;
namespaceDataContractJsonDeserializer { [DataContract] publicclassPerson { [DataMember] publicstring Name { get; set; } [DataMember] public Dog dog; } publicclassDog { publicstring Name { get; set; } }