---
# System prepended metadata

title: '`Newtonsoft.Json` で、`private` Setter を使ってデシリアライズする 2021-12-16'
tags: [Newtonsoft.Json, Setter, private]

---

---
lang: ja-jp
breaks: true
---
# `Newtonsoft.Json` で、`private` Setter を使ってデシリアライズする 2021-12-16

```csharp=
public class Sample
{
    private string m_name;
    public Sample(string _name)    // '_'が付加されているので駄目
    {
        m_name = _name;
    }
    public string Name
    {
        get => m_name;
        private set => m_name = value;    // これがないと駄目
    }
}
```

```xml=
  <ItemGroup>
    <PackageReference Include="JsonNet.ContractResolvers" Version="2.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
  </ItemGroup>
```

```csharp=
Formatting formatting = Formatting.Indented;
string json = JsonConvert.SerializeObject(sample, formatting);
Console.WriteLine(json);
        
var settings = new JsonSerializerSettings
{
    ContractResolver = new PrivateSetterContractResolver()
};
Sample dec = JsonConvert.DeserializeObject<Sample>(json, settings);
Console.WriteLine(JsonConvert.SerializeObject(dec, formatting));
```

###### tags: `Newtonsoft.Json` `private` `Setter`
