本文整理汇总了C#中JsonWriter类的典型用法代码示例。如果您正苦于以下问题:C# JsonWriter类的具体用法?C# JsonWriter怎么用?C# JsonWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonWriter类属于命名空间,在下文中一共展示了JsonWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportTable
internal static void ExportTable(DataTable table, JsonWriter writer)
{
Debug.Assert(table != null);
Debug.Assert(writer != null);
DataViewExporter.ExportView(table.DefaultView, writer);
}
示例2: 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)
{
EnsureReflectionObject(value.GetType());
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
string keyName = (string)_reflectionObject.GetValue(value, KeyPropertyName);
object keyValue = _reflectionObject.GetValue(value, ValuePropertyName);
Type keyValueType = (keyValue != null) ? keyValue.GetType() : null;
writer.WriteStartObject();
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName);
writer.WriteValue(keyName);
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName);
writer.WriteValue((keyValueType != null) ? keyValueType.FullName : null);
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValuePropertyName) : ValuePropertyName);
if (keyValueType != null)
{
string valueJson;
if (JsonSerializerInternalWriter.TryConvertToString(keyValue, keyValueType, out valueJson))
writer.WriteValue(valueJson);
else
writer.WriteValue(keyValue);
}
else
{
writer.WriteNull();
}
writer.WriteEndObject();
}
示例3: 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)
{
long ticks;
if (value is DateTime)
{
DateTime dateTime = (DateTime)value;
DateTime utcDateTime = dateTime.ToUniversalTime();
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTime);
}
#if !PocketPC && !NET20
else if (value is DateTimeOffset)
{
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
DateTimeOffset utcDateTimeOffset = dateTimeOffset.ToUniversalTime();
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTimeOffset.UtcDateTime);
}
#endif
else
{
throw new JsonSerializationException("Expected date object value.");
}
writer.WriteStartConstructor("Date");
writer.WriteValue(ticks);
writer.WriteEndConstructor();
}
示例4: 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)
{
if (value == null)
{
writer.WriteNull();
return;
}
Enum e = (Enum)value;
string enumName = e.ToString("G");
if (char.IsNumber(enumName[0]) || enumName[0] == '-')
{
// enum value has no name so write number
writer.WriteValue(value);
}
else
{
Type enumType = e.GetType();
string finalName = EnumUtils.ToEnumName(enumType, enumName, CamelCaseText);
writer.WriteValue(finalName);
}
}
示例5: 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)
{
if (value == null)
{
writer.WriteNull();
return;
}
Enum e = (Enum)value;
string enumName = e.ToString("G");
if (char.IsNumber(enumName[0]) || enumName[0] == '-')
{
writer.WriteValue(value);
}
else
{
BidirectionalDictionary<string, string> map = GetEnumNameMap(e.GetType());
string resolvedEnumName;
map.TryGetByFirst(enumName, out resolvedEnumName);
resolvedEnumName = resolvedEnumName ?? enumName;
if (CamelCaseText)
{
string[] names = resolvedEnumName.Split(',').Select(item => StringUtils.ToCamelCase(item.Trim())).ToArray();
resolvedEnumName = string.Join(", ", names);
}
writer.WriteValue(resolvedEnumName);
}
}
示例6: WritesNonEscapedString
public void WritesNonEscapedString()
{
var sw = new StringWriter();
var writer = new JsonWriter(sw);
writer.WriteValue("hello, world");
Assert.AreEqual("\"hello, world\"", sw.ToString());
}
示例7: WritesEscapedControlCharacters
public void WritesEscapedControlCharacters()
{
var sw = new StringWriter();
var writer = new JsonWriter(sw);
writer.WriteValue("this is escaped: \b\f\n\r\t\x17");
Assert.AreEqual("\"this is escaped: \\b\\f\\n\\r\\t\\x17\"", sw.ToString());
}
示例8: ValueFormatting
public void ValueFormatting()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonWriter(sw))
{
jsonWriter.WriteStartArray();
jsonWriter.WriteValue('@');
jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
jsonWriter.WriteValue(true);
jsonWriter.WriteValue(10);
jsonWriter.WriteValue(10.99);
jsonWriter.WriteValue(0.99);
jsonWriter.WriteValue(0.000000000000000001d);
jsonWriter.WriteValue(0.000000000000000001m);
jsonWriter.WriteValue(null);
jsonWriter.WriteValue("This is a string.");
jsonWriter.WriteNull();
jsonWriter.WriteUndefined();
jsonWriter.WriteEndArray();
}
const string expected = @"[""@"",""\r\n\t\f\b?{\\r\\n\""'"",true,10,10.99,0.99,1E-18,0.000000000000000001,,""This is a string."",null,undefined]";
string result = sb.ToString();
Console.WriteLine("ValueFormatting");
Console.WriteLine(result);
Assert.AreEqual(expected, result);
}
示例9: WriteResponseForPostJson
void WriteResponseForPostJson(HttpRequest request, HttpResponse response)
{
// read request JSON
uint requestedCount = ReadCount(request.Body);
// write response JSON
var json = new JsonWriter<ResponseFormatter>(response.Body, prettyPrint: false);
json.WriteObjectStart();
json.WriteArrayStart();
for (int i = 0; i < requestedCount; i++) {
json.WriteString("hello!");
}
json.WriteArrayEnd();
json.WriteObjectEnd();
// write headers
var headers = response.Headers;
headers.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK"));
headers.Append("Content-Length : ");
headers.Append(response.Body.CommitedBytes);
headers.AppendHttpNewLine();
headers.Append("Content-Type : text/plain; charset=UTF-8");
headers.AppendHttpNewLine();
headers.Append("Server : .NET Core Sample Server");
headers.AppendHttpNewLine();
headers.Append("Date : ");
headers.Append(DateTime.UtcNow, 'R');
headers.AppendHttpNewLine();
headers.AppendHttpNewLine();
}
示例10: WriteJson
public override void WriteJson(JsonWriter writer, object value)
{
DateTime dateTime = (DateTime)value;
long javaScriptTicks = JavaScriptConvert.ConvertDateTimeToJavaScriptTicks(dateTime);
writer.WriteValue("@" + javaScriptTicks.ToString(null, CultureInfo.InvariantCulture) + "@");
}
示例11: Write
/// <summary>
/// Write this target to a JsonWriter
/// </summary>
/// <param name="Writer">Writer to output to</param>
void Write(JsonWriter Writer)
{
Writer.WriteObjectStart();
Writer.WriteValue("Name", Name);
Writer.WriteValue("LoadingPolicy", LoadingPolicy.ToString());
Writer.WriteObjectEnd();
}
示例12: ExportValue
protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
{
Debug.Assert(value != null);
Debug.Assert(writer != null);
ExportTime((DateTime) value, writer);
}
示例13: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dataResult = value as DataResult;
#region 序列化DataResult对象
writer.WriteStartObject();
// 写total属性
writer.WritePropertyName("total");
serializer.Serialize(writer, dataResult.Total);
// 写pagecount属性
writer.WritePropertyName("pagecount");
serializer.Serialize(writer, dataResult.PageCount);
// 写pageindex属性
writer.WritePropertyName("pageindex");
serializer.Serialize(writer, dataResult.PageIndex);
// 写rows属性
var converter = new DataTableConverter();
writer.WritePropertyName("rows");
converter.WriteJson(writer, dataResult.Data, serializer);
writer.WriteEndObject();
#endregion
}
示例14: ExportValue
protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
{
Debug.Assert(context != null);
Debug.Assert(value != null);
Debug.Assert(writer != null);
if (_properties.Count == 0)
{
writer.WriteString(value.ToString());
}
else
{
writer.WriteStartObject();
foreach (PropertyDescriptor property in _properties)
{
object propertyValue = property.GetValue(value);
if (!JsonNull.LogicallyEquals(propertyValue))
{
writer.WriteMember(property.Name);
context.Export(propertyValue, writer);
}
}
writer.WriteEndObject();
}
}
示例15: WriteToService
public void WriteToService()
{
System.Text.StringBuilder m_write = new System.Text.StringBuilder();
JsonWriter m_writer = new JsonWriter(m_write);
m_writer.WriteObjectStart();
m_writer.WritePropertyName("ButtonID");
}