本文整理汇总了C#中JsonSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# JsonSerializer.Serialize方法的具体用法?C# JsonSerializer.Serialize怎么用?C# JsonSerializer.Serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonSerializer
的用法示例。
在下文中一共展示了JsonSerializer.Serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NullSerialize
public void NullSerialize()
{
var Serializer = new JsonSerializer();
Assert.Equal("", Serializer.Serialize(typeof(object), null));
Assert.Equal("", Serializer.Serialize(null, new { A = "" }));
Assert.Equal("", Serializer.Serialize(null, null));
}
示例2: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var analysisValue = (AnalysisSettings)value;
writer.WriteStartObject();
{
if (analysisValue.Analyzers.Count > 0)
{
writer.WritePropertyName("analyzer");
serializer.Serialize(writer, analysisValue.Analyzers);
}
if (analysisValue.TokenFilters.Count > 0)
{
writer.WritePropertyName("filter");
serializer.Serialize(writer, analysisValue.TokenFilters);
}
if (analysisValue.Tokenizers.Count > 0)
{
writer.WritePropertyName("tokenizer");
serializer.Serialize(writer, analysisValue.Tokenizers);
}
if (analysisValue.CharFilters.Count > 0)
{
writer.WritePropertyName("char_filter");
serializer.Serialize(writer, analysisValue.CharFilters);
}
}
writer.WriteEndObject();
}
示例3: TryWriteFeature
public bool TryWriteFeature(JsonWriter writer, object value, JsonSerializer serializer)
{
if (!_shapeConverter.CanConvert(ObjectType.Feature))
return false;
ObjectType geomType = _shapeConverter.GetObjectType(value);
if (geomType != ObjectType.Feature)
return false;
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(Enum.GetName(typeof(ObjectType), ObjectType.Feature));
object id;
Dictionary<string, object> props;
var geometry = _shapeConverter.FromFeature(value, out id, out props);
if (id != null)
{
writer.WritePropertyName("id");
serializer.Serialize(writer, id);
}
if (props != null && props.Count > 0)
{
writer.WritePropertyName("properties");
serializer.Serialize(writer, props);
}
writer.WritePropertyName("geometry");
serializer.Serialize(writer, geometry);
writer.WriteEndObject();
return true;
}
示例4: Serialize
public void Serialize()
{
var Serializer = new JsonSerializer();
dynamic Object = new { A = 5, B = "ASDF" };
Assert.Equal("{\"A\":5,\"B\":\"ASDF\"}", Serializer.Serialize(Object.GetType(), Object));
Object = new TestObject { A = 5, B = "ASDF" };
Assert.Equal("{\"A\":5,\"B\":\"ASDF\"}", Serializer.Serialize(Object.GetType(), Object));
}
示例5: TestGetPlaylist
public void TestGetPlaylist()
{
var playlists = soundCloudClient.User(settings.TestUserId).GetPlaylists();
if (playlists.Length == 0)
return;
var playlist = soundCloudClient.Playlist(playlists[0].Id).GetPlaylist();
var jsonSerializer = new JsonSerializer();
Assert.AreEqual(jsonSerializer.Serialize(playlists[0]), jsonSerializer.Serialize(playlist));
}
示例6: WriteJson
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
ReflectionObject reflectionObject = ReflectionObjectPerType.Get(value.GetType());
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
writer.WriteStartObject();
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyName) : KeyName);
serializer.Serialize(writer, reflectionObject.GetValue(value, KeyName), reflectionObject.GetType(KeyName));
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValueName) : ValueName);
serializer.Serialize(writer, reflectionObject.GetValue(value, ValueName), reflectionObject.GetType(ValueName));
writer.WriteEndObject();
}
示例7: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var mapping = (WarmerMapping) value;
writer.WriteStartObject();
writer.WritePropertyName("types");
serializer.Serialize(writer, mapping.Types);
writer.WritePropertyName("source");
serializer.Serialize(writer, mapping.Source);
writer.WriteEndObject();
}
示例8: WriteJson
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Type t = value.GetType();
PropertyInfo keyProperty = t.GetProperty("Key");
PropertyInfo valueProperty = t.GetProperty("Value");
writer.WriteStartObject();
writer.WritePropertyName("Key");
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(keyProperty, value));
writer.WritePropertyName("Value");
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(valueProperty, value));
writer.WriteEndObject();
}
示例9: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var f = value as ITermsBaseFilter;
if (f == null || (f.IsConditionless && !f.IsVerbatim)) return;
var contract = serializer.ContractResolver as SettingsContractResolver;
if (contract == null)
return;
var field = contract.Infer.PropertyPath(f.Field);
if (field.IsNullOrEmpty())
return;
writer.WriteStartObject();
{
var lf = f as ITermsLookupFilter;
if (lf != null)
{
writer.WritePropertyName(field);
writer.WriteStartObject();
{
WriteProperty(writer, f, "id", lf.Id);
SerializeProperty(writer, serializer, f, "type", lf.Type);
SerializeProperty(writer, serializer, f, "index", lf.Index);
SerializeProperty(writer, serializer, f, "path", lf.Path);
WriteProperty(writer, f, "routing", lf.Routing);
WriteProperty(writer, f, "cache", lf.CacheLookup);
}
writer.WriteEndObject();
}
var tf = f as ITermsFilter;
if (tf != null)
{
writer.WritePropertyName(field);
serializer.Serialize(writer, tf.Terms);
}
if (f.Execution.HasValue)
{
writer.WritePropertyName("execution");
serializer.Serialize(writer, f.Execution.Value);
}
WriteProperty(writer, f, "_cache", f.Cache);
WriteProperty(writer, f, "_cache_key", f.CacheKey);
WriteProperty(writer, f, "_name", f.FilterName);
}
writer.WriteEndObject();
}
示例10: WriteJson
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="T:Raven.Imports.Newtonsoft.Json.JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
if (value.GetType() == typeof (object))
{
writer.WriteStartObject();
writer.WriteEndObject();
return;
}
var dynamicValue = ((IDynamicMetaObjectProvider) value).GetMetaObject(Expression.Constant(value));
writer.WriteStartObject();
foreach (var dynamicMemberName in dynamicValue.GetDynamicMemberNames())
{
writer.WritePropertyName(dynamicMemberName);
var memberValue = DynamicUtil.GetValueDynamically(value, dynamicMemberName);
if(memberValue == null || memberValue is ValueType || memberValue is string)
writer.WriteValue(memberValue);
else
serializer.Serialize(writer, memberValue);
}
writer.WriteEndObject();
}
示例11: WhenSerializeWithMoreDatatypes_GetSerialiedJsonString
public void WhenSerializeWithMoreDatatypes_GetSerialiedJsonString()
{
// Arrange
var jsonSerializer = new JsonSerializer();
var jsonMoreDataTypes = new JsonMoreDataTypes
{
BoolProperty = true,
ByteProperty = 1,
SByteProperty = 2,
CharProperty = 'K',
DoubleProperty = 12.52,
FloatProperty = 12.52F,
IntProperty = 15789,
UIntProperty = 1234,
LongProperty = 152,
ULongProperty = 12345,
ShortProperty = 123,
UShortProperty = 555,
StringProperty = "This is a string"
};
// Act
var result = jsonSerializer.Serialize(jsonMoreDataTypes);
// Assert
Assert.AreEqual("{\"boolProperty\": true,\"byteProperty\": 1,\"sByteProperty\": 2,\"charProperty\": \"K\",\"doubleProperty\": 12.52,\"floatProperty\": 12.5200005,\"intProperty\": 15789,\"uIntProperty\": 1234,\"longProperty\": 152,\"uLongProperty\": 12345,\"shortProperty\": 123,\"uShortProperty\": 555,\"stringProperty\": \"This is a string\"}", result);
}
示例12: WriteLargeJson
public static void WriteLargeJson()
{
var json = System.IO.File.ReadAllText("large.json");
IList<PerformanceTests.RootObject> o = JsonConvert.DeserializeObject<IList<PerformanceTests.RootObject>>(json);
Console.WriteLine("Press any key to start serialize");
Console.ReadKey();
Console.WriteLine("Serializing...");
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 100; i++)
{
using (StreamWriter file = System.IO.File.CreateText("largewrite.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
serializer.Serialize(file, o);
}
}
sw.Stop();
Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds);
}
示例13: CyclicalObject
public void CyclicalObject()
{
var serializer = new JsonSerializer();
var a = new CyclicalObject_Class();
a.A = a;
string result = serializer.Serialize(a);
}
示例14: AClassDecoratedWithTheEncryptAttributeIsEncrypted
public void AClassDecoratedWithTheEncryptAttributeIsEncrypted()
{
var encryptionMechanism = new Base64EncryptionMechanism();
var configuration = new JsonSerializerConfiguration
{
EncryptionMechanism = encryptionMechanism,
};
var serializer = new JsonSerializer<Grault>(configuration);
var instance = new Grault
{
Qux = "abc",
Garply = true
};
var json = serializer.Serialize(instance);
var expected =
@""""
+ encryptionMechanism.Encrypt(@"{""Qux"":""abc"",""Garply"":true}")
+ @"""";
Assert.That(json, Is.EqualTo(expected));
}
示例15: CanSerializeWithEncryptRootObjectEnabled
public void CanSerializeWithEncryptRootObjectEnabled()
{
var encryptionMechanism = new Base64EncryptionMechanism();
var configuration = new JsonSerializerConfiguration
{
EncryptionMechanism = encryptionMechanism,
EncryptRootObject = true
};
var serializer = new JsonSerializer<Bar>(configuration);
var instance = new Bar
{
Baz = new Baz
{
Qux = "abc",
Garply = true
},
Corge = 123.45
};
var json = serializer.Serialize(instance);
var expected =
@""""
+ encryptionMechanism.Encrypt(@"{""Baz"":{""Qux"":""abc"",""Garply"":true},""Corge"":123.45}")
+ @"""";
Assert.That(json, Is.EqualTo(expected));
}