从内存中的字符串值反序列化为 DataContract
Deserializing as DataContract from string value in memory
我有以下代码(有效)来反序列化从网络调用接收到的原始 json:
public static async Task<Example> GetExample() {
Example record = new Example();
using ( WebClient wc = new WebClient() ) {
wc.Headers.Add( "Accept", "application/json" );
try {
DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof( Example ) );
using ( Stream s = await wc.OpenReadTaskAsync( "https://example.com/sample.json" ) ) {
record = ser.ReadObject( s ) as Example;
}
} catch ( SerializationException se ) {
Debug.WriteLine( se.Message );
} catch ( WebException we ) {
Debug.WriteLine( we.Message );
} catch ( Exception e ) {
Debug.WriteLine( e.Message );
}
}
return record;
}
但是,我有一个不同的场景,我正在使用的数据是加密的,所以我需要解码 base64,然后解密结果以获得 json 数据。
为简单起见,假设以下是从服务器接收到的字符串(仅 base64 编码):
ew0KICAidG9tIjogIjEyMyINCn0=
解码为(存储在foo
)
string foo = Convert.FromBase64String("ew0KICAidG9tIjogIjEyMyINCn0=");
如何将 foo
传递给 .ReadObject()
,因为 .ReadObject()
只接受 Stream
试试 yhis:
using ( Stream s = await wc.OpenReadTaskAsync( "https://example.com/sample.json" ) )
{
string str = Encoding.UTF8.GetString(s.GetBuffer(),0 , s.GetBuffer().Length)
string foo = Convert.FromBase64String(str);
}
将其写回流并将流传递给 ReadObject
。您可以按照 here .
所述使用 MemoryStream
以下是匿名类型方法的示例:
/// <summary>
/// Read json from string into class with DataContract properties
/// </summary>
/// <typeparam name="T">DataContract class</typeparam>
/// <param name="json">JSON as a string</param>
/// <param name="encoding">Text encoding format (example Encoding.UTF8)</param>
/// <param name="settings">DataContract settings (can be used to set datetime format, etc)</param>
/// <returns>DataContract class populated with serialized json data</returns>
public static T FromString<T>( string json, Encoding encoding, DataContractJsonSerializerSettings settings ) where T : class {
T result = null;
try {
DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof( T ), settings );
using ( Stream s = new MemoryStream( ( encoding ?? Encoding.UTF8 ).GetBytes( json ?? "" ) ) ) {
result = ser.ReadObject( s ) as T;
}
} catch ( SerializationException se ) {
Debug.WriteLine( se.Message );
} catch ( Exception e ) {
Debug.WriteLine( e.Message );
}
return result;
}
我有以下代码(有效)来反序列化从网络调用接收到的原始 json:
public static async Task<Example> GetExample() {
Example record = new Example();
using ( WebClient wc = new WebClient() ) {
wc.Headers.Add( "Accept", "application/json" );
try {
DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof( Example ) );
using ( Stream s = await wc.OpenReadTaskAsync( "https://example.com/sample.json" ) ) {
record = ser.ReadObject( s ) as Example;
}
} catch ( SerializationException se ) {
Debug.WriteLine( se.Message );
} catch ( WebException we ) {
Debug.WriteLine( we.Message );
} catch ( Exception e ) {
Debug.WriteLine( e.Message );
}
}
return record;
}
但是,我有一个不同的场景,我正在使用的数据是加密的,所以我需要解码 base64,然后解密结果以获得 json 数据。
为简单起见,假设以下是从服务器接收到的字符串(仅 base64 编码):
ew0KICAidG9tIjogIjEyMyINCn0=
解码为(存储在foo
)
string foo = Convert.FromBase64String("ew0KICAidG9tIjogIjEyMyINCn0=");
如何将 foo
传递给 .ReadObject()
,因为 .ReadObject()
只接受 Stream
试试 yhis:
using ( Stream s = await wc.OpenReadTaskAsync( "https://example.com/sample.json" ) )
{
string str = Encoding.UTF8.GetString(s.GetBuffer(),0 , s.GetBuffer().Length)
string foo = Convert.FromBase64String(str);
}
将其写回流并将流传递给 ReadObject
。您可以按照 here .
MemoryStream
以下是匿名类型方法的示例:
/// <summary>
/// Read json from string into class with DataContract properties
/// </summary>
/// <typeparam name="T">DataContract class</typeparam>
/// <param name="json">JSON as a string</param>
/// <param name="encoding">Text encoding format (example Encoding.UTF8)</param>
/// <param name="settings">DataContract settings (can be used to set datetime format, etc)</param>
/// <returns>DataContract class populated with serialized json data</returns>
public static T FromString<T>( string json, Encoding encoding, DataContractJsonSerializerSettings settings ) where T : class {
T result = null;
try {
DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof( T ), settings );
using ( Stream s = new MemoryStream( ( encoding ?? Encoding.UTF8 ).GetBytes( json ?? "" ) ) ) {
result = ser.ReadObject( s ) as T;
}
} catch ( SerializationException se ) {
Debug.WriteLine( se.Message );
} catch ( Exception e ) {
Debug.WriteLine( e.Message );
}
return result;
}