我有下一个反序列化/序列化对象的代码:
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace api
{
static class JSONHelper
{
//Generate JSON only with ID and revision
private class ContractResolverSaveIdAndRevision : DefaultContractResolver
{
private readonly List<string> allowedPropNames;
private readonly string startingSymbol;
public ContractResolverSaveIdAndRevision()
{
allowedPropNames = new List<string>();
allowedPropNames.Add( Constants._id );
allowedPropNames.Add( Constants.__rev );
startingSymbol = "_";
}
protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization )
{
IList<JsonProperty> properties = base.CreateProperties( type, memberSerialization );
return collectAllowedProperties( properties, allowedPropNames, startingSymbol );
}
}
//Generate JSON only with ID
private class ContractResolverSaveId : DefaultContractResolver
{
private readonly List<string> allowedPropNames;
private readonly string startingSymbol;
public ContractResolverSaveId()
{
allowedPropNames = new List<string>();
allowedPropNames.Add( Constants._id );
startingSymbol = "_";
}
protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization )
{
IList<JsonProperty> properties = base.CreateProperties( type, memberSerialization );
return collectAllowedProperties( properties, allowedPropNames, startingSymbol );
}
}
private static readonly JsonSerializer jsonSerializerWithoutSettings = new JsonSerializer();
private static readonly ContractResolverSaveIdAndRevision saveIdAndRevisionContractResolver = new ContractResolverSaveIdAndRevision();
private static readonly JsonSerializerSettings settingsWithIdAndRevision = new JsonSerializerSettings { ContractResolver = saveIdAndRevisionContractResolver };
private static readonly JsonSerializer jsonSerializerWithIdAndRevision = JsonSerializer.Create( settingsWithIdAndRevision );
private static readonly ContractResolverSaveId saveIdContractResolver = new ContractResolverSaveId();
private static readonly JsonSerializerSettings settingsWithId = new JsonSerializerSettings { ContractResolver = saveIdContractResolver };
private static readonly JsonSerializer jsonSerializerWithId = JsonSerializer.Create( settingsWithId );
public static object DeserializeJson( Type type, string json )
{
lock ( jsonSerializerWithoutSettings )
{
object obj = null;
try
{
StringReader stringReader = new StringReader( json );
JsonTextReader jsonTextReader = new JsonTextReader( stringReader );
obj = jsonSerializerWithoutSettings.Deserialize( jsonTextReader, type );
jsonTextReader.Close();
stringReader.Dispose();
jsonTextReader = null;
stringReader = null;
}
catch ( Exception e )
{
Debug.DebugPrinter.PrintError( e );
}
return obj;
}
}
public static T DeserializeJson<T>( string json )
{
lock ( jsonSerializerWithoutSettings )
{
T obj = default( T );
try
{
StringReader stringReader = new StringReader( json );
JsonTextReader jsonTextReader = new JsonTextReader( stringReader );
obj = jsonSerializerWithoutSettings.Deserialize<T>( jsonTextReader );
jsonTextReader.Close();
stringReader.Dispose();
jsonTextReader = null;
stringReader = null;
}
catch ( Exception e )
{
Debug.DebugPrinter.PrintError( e );
}
return obj;
}
}
public static string SerializeJson( object value )
{
lock ( jsonSerializerWithIdAndRevision )
{
string json = string.Empty;
try
{
StringWriter stringWriter = new StringWriter();
JsonTextWriter jsonTextWriter = new JsonTextWriter( stringWriter );
jsonSerializerWithIdAndRevision.Serialize( jsonTextWriter, value );
json = stringWriter.ToString();
jsonTextWriter.Close();
stringWriter.Dispose();
jsonTextWriter = null;
stringWriter = null;
}
catch ( Exception e )
{
Debug.DebugPrinter.PrintError( e );
}
return json;
}
}
public static string SerializeJsonWithoutRevision( object value )
{
lock ( jsonSerializerWithId )
{
string json = string.Empty;
try
{
StringWriter stringWriter = new StringWriter();
JsonTextWriter jsonTextWriter = new JsonTextWriter( stringWriter );
jsonSerializerWithId.Serialize( jsonTextWriter, value );
json = stringWriter.ToString();
jsonTextWriter.Close();
stringWriter.Dispose();
jsonTextWriter = null;
stringWriter = null;
}
catch ( Exception e )
{
Debug.DebugPrinter.PrintError( e );
}
return json;
}
}
private static IList<JsonProperty> collectAllowedProperties( IList<JsonProperty> objectProperties, IList<string> allowedPropNames, string startingSymbol )
{
return objectProperties.Where( p => allowedPropNames.Contains( p.PropertyName ) || !p.PropertyName.StartsWith( startingSymbol ) ).ToList();
}
}
}
我启动了有关内存的Visual Studio配置文件和集合信息。我从Newtonsoft.Json.dll发现了很多对象。而且我不知道如何解决它或清除记忆。
你知道我怎么能改善这种行为吗?
由于