-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Globalization; | ||
using Newtonsoft.Json; | ||
|
||
namespace XrmEntitySerializer | ||
{ | ||
public class IntegerConverter : ValueTypeConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(int) == objectType || typeof(int?) == objectType; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("$type"); | ||
writer.WriteValue(string.Format("{0}, {1}", value.GetType().FullName, value.GetType().Assembly.GetName().Name)); | ||
writer.WritePropertyName("$value"); | ||
writer.WriteValue(value.ToString()); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
protected override object ReadValue(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
int result; | ||
if (reader.TokenType == JsonToken.Null) | ||
{ | ||
if (objectType != typeof(int?)) | ||
{ | ||
throw new JsonSerializationException(String.Format(CultureInfo.InvariantCulture, "Cannot convert null value to {0}.", objectType)); | ||
} | ||
return default(int?); | ||
} | ||
else if (!int.TryParse(reader.Value.ToString(), out result)) | ||
{ | ||
throw new JsonSerializationException(string.Format(CultureInfo.InvariantCulture, "Unexpected token or value when parsing Integer. Token: {0}, Value: {1}", reader.TokenType, reader.Value)); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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