Created
August 16, 2013 06:22
-
-
Save shadeglare/6247714 to your computer and use it in GitHub Desktop.
Serialization and deserialization on byte array property via ServiceStack.Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using ServiceStack.Text; | |
public sealed class User | |
{ | |
public String FirstName { get; set; } | |
public String LastName { get; set; } | |
public Byte[] RawData { get; set; } | |
} | |
public sealed class SpecificNode | |
{ | |
public String Company { get; set; } | |
public DateTime? EstablishmentDate { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var user = new User | |
{ | |
FirstName = "Vasya", | |
LastName = "Pupkin", | |
}; | |
var specificNode = new SpecificNode | |
{ | |
Company = "SNP HOLDING GROUP", | |
EstablishmentDate = DateTime.Now, | |
}; | |
var serializedSpecificNode = specificNode.ToJson(); | |
var rawSpecificNode = Encoding.UTF8.GetBytes(serializedSpecificNode); | |
user.RawData = rawSpecificNode; | |
var serializedUser = user.ToJson(); | |
Console.WriteLine(serializedUser); | |
var newUser = JsonSerializer.DeserializeFromString<User>(serializedUser); | |
var newSerializedSpecificNode = Encoding.UTF8.GetString(newUser.RawData); | |
var newSpecificNode = JsonSerializer.DeserializeFromString<SpecificNode>(newSerializedSpecificNode); | |
Console.WriteLine(newSpecificNode.Company); | |
Console.WriteLine(newSpecificNode.EstablishmentDate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment