当前位置: 首页>>代码示例>>C#>>正文


C# JsonWriter类代码示例

本文整理汇总了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);
       }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:7,代码来源:DataTableExporter.cs

示例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();
        }
开发者ID:BgCircle,项目名称:Newtonsoft.Json,代码行数:40,代码来源:EntityKeyMemberConverter.cs

示例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();
    }
开发者ID:925coder,项目名称:ravendb,代码行数:33,代码来源:JavaScriptDateTimeConverter.cs

示例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);
            }
        }
开发者ID:b-bot-110,项目名称:Newtonsoft.Json,代码行数:32,代码来源:StringEnumConverter.cs

示例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);
      }
    }
开发者ID:GotenXiao,项目名称:blink1,代码行数:39,代码来源:StringEnumConverter.cs

示例6: WritesNonEscapedString

 public void WritesNonEscapedString()
 {
     var sw = new StringWriter();
     var writer = new JsonWriter(sw);
     writer.WriteValue("hello, world");
     Assert.AreEqual("\"hello, world\"", sw.ToString());
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Strings.cs

示例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());
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Strings.cs

示例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);
    }
开发者ID:webgrid,项目名称:WebGrid,代码行数:31,代码来源:JsonWriterTest.cs

示例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();
    }
开发者ID:geoffkizer,项目名称:corefxlab,代码行数:30,代码来源:SampleServer.cs

示例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) + "@");
    }
开发者ID:Belxjander,项目名称:Asuna,代码行数:7,代码来源:AspNetAjaxDateTimeConverter.cs

示例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();
		}
开发者ID:zhaoyizheng0930,项目名称:UnrealEngine,代码行数:11,代码来源:LocalizationDescriptor.cs

示例12: ExportValue

 protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
 {
     Debug.Assert(value != null);
     Debug.Assert(writer != null);
     
     ExportTime((DateTime) value, writer);
 }
开发者ID:liuxing7954,项目名称:cangku_1,代码行数:7,代码来源:DateTimeExporter.cs

示例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
    }
开发者ID:guyuefeng,项目名称:aspnet-sample,代码行数:25,代码来源:JsonHelper3.cs

示例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();
            }
        }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:28,代码来源:ComponentExporter.cs

示例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");
 }
开发者ID:Venbb,项目名称:Games,代码行数:7,代码来源:jsoninfo.cs


注:本文中的JsonWriter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。