Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
Last active July 26, 2020 11:30
Show Gist options
  • Save jsakamoto/471e84fc739919326c6a849ebf216086 to your computer and use it in GitHub Desktop.
Save jsakamoto/471e84fc739919326c6a849ebf216086 to your computer and use it in GitHub Desktop.
Study of System.Text.Json
{
"sdk": {
"version": "3.1.300",
"rollForward": "latestPatch"
}
}
namespace StudyOfSystemTextJson
{
public class Person
{
public string Name { get; }
public int Age { get; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
public override string ToString() => $"{Name}, {Age}";
}
}
using System;
using System.Text.Json;
namespace StudyOfSystemTextJson
{
class Program
{
static void Main(string[] args)
{
var person = new Person(
name: "Taro",
age: 23
);
var jsonText = JsonSerializer.Serialize(person);
Console.WriteLine(jsonText);
var newPerson = JsonSerializer.Deserialize<Person>(jsonText);
Console.WriteLine(newPerson.ToString());
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="5.0.0-preview.7.20364.11" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment