-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs/tip: Gist for JSON serializing with JSON.Net #21
Comments
What version of JSON.NET are you using? Can you post a snippet of the raw json it creates? |
Environment Scenario public class Manifest
{
public string Name { get; set; }
public SemVersion Version { get; set; }
} var manifest = new Manifest
{
Name = "foo-bar",
Version = SemVersion.Parse("1.0.0")
};
var json = JsonConvert.SerializeObject(manifest, Formatting.Indented); output {
"Name": "foo-bar",
"Version": {
"SemVersion": "1.0.0"
}
} Expected output {
"Name": "foo-bar",
"Version": "1.0.0"
} Workaround var manifest = new Manifest
{
Name = "foo-bar",
Version = SemVersion.Parse("1.0.0")
};
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new SemVersionConverter()); // SemVesrionConverter https://gist.github.com/madaz/efab4a5554b88dc2862d58046ddba00f
var json = JsonConvert.SerializeObject(manifest, Formatting.Indented, settings); Note: Deserialize works as expected using JSON.Net with out any workarounds string raw = @"{
""Name"": ""foo-bar"",
""Version"": ""1.0.0""
}";
var manifest = JsonConvert.DeserializeObject<Manifest>(raw);
Trace.WriteLine(manifest.Version == SemVersion.Parse("1.0.0")); |
Would be great to have this included in the semver library, enabled by default as when you serialize the classic old |
Why the |
Custom converter for serializing to Json using http://www.newtonsoft.com/json.
This is an enhancement of the
Newton.Json.Converters.VersionConverter
.https://gist.github.com/madaz/efab4a5554b88dc2862d58046ddba00f
Reason
Without the custom serializer, the default would create a nested "SemVersion" property inside the original property when serializing.
The default deserialzing worked fine.
The text was updated successfully, but these errors were encountered: