| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System;
- using Newtonsoft.Json;
- namespace Abp.Json
- {
- /// <summary>
- /// Defines helper methods to work with JSON.
- /// </summary>
- public static class JsonSerializationHelper
- {
- private const char TypeSeperator = '|';
- /// <summary>
- /// Serializes an object with a type information included.
- /// So, it can be deserialized using <see cref="DeserializeWithType"/> method later.
- /// </summary>
- public static string SerializeWithType(object obj)
- {
- return SerializeWithType(obj, obj.GetType());
- }
- /// <summary>
- /// Serializes an object with a type information included.
- /// So, it can be deserialized using <see cref="DeserializeWithType"/> method later.
- /// </summary>
- public static string SerializeWithType(object obj, Type type)
- {
- var serialized = obj.ToJsonString();
- return string.Format(
- "{0}{1}{2}",
- type.AssemblyQualifiedName,
- TypeSeperator,
- serialized
- );
- }
- /// <summary>
- /// Deserializes an object serialized with <see cref="SerializeWithType(object)"/> methods.
- /// </summary>
- public static T DeserializeWithType<T>(string serializedObj)
- {
- return (T)DeserializeWithType(serializedObj);
- }
- /// <summary>
- /// Deserializes an object serialized with <see cref="SerializeWithType(object)"/> methods.
- /// </summary>
- public static object DeserializeWithType(string serializedObj)
- {
- var typeSeperatorIndex = serializedObj.IndexOf(TypeSeperator);
- var type = Type.GetType(serializedObj.Substring(0, typeSeperatorIndex));
- var serialized = serializedObj.Substring(typeSeperatorIndex + 1);
- var options = new JsonSerializerSettings
- {
- ContractResolver = new AbpCamelCasePropertyNamesContractResolver()
- };
- return JsonConvert.DeserializeObject(serialized, type, options);
- }
- }
- }
|