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


C# JsonTextWriter.WriteValue方法代码示例

本文整理汇总了C#中JsonTextWriter.WriteValue方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextWriter.WriteValue方法的具体用法?C# JsonTextWriter.WriteValue怎么用?C# JsonTextWriter.WriteValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JsonTextWriter的用法示例。


在下文中一共展示了JsonTextWriter.WriteValue方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Example

        public void Example()
        {
            #region Usage
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter writer = new JsonTextWriter(sw))
            {
                writer.Formatting = Formatting.Indented;

                writer.WriteStartObject();
                writer.WritePropertyName("CPU");
                writer.WriteValue("Intel");
                writer.WritePropertyName("PSU");
                writer.WriteValue("500W");
                writer.WritePropertyName("Drives");
                writer.WriteStartArray();
                writer.WriteValue("DVD read/writer");
                writer.WriteComment("(broken)");
                writer.WriteValue("500 gigabyte hard drive");
                writer.WriteValue("200 gigabype hard drive");
                writer.WriteEnd();
                writer.WriteEndObject();
            }

            Console.WriteLine(sb.ToString());
            // {
            //   "CPU": "Intel",
            //   "PSU": "500W",
            //   "Drives": [
            //     "DVD read/writer"
            //     /*(broken)*/,
            //     "500 gigabyte hard drive",
            //     "200 gigabype hard drive"
            //   ]
            // }
            #endregion

            Assert.AreEqual(@"{
  ""CPU"": ""Intel"",
  ""PSU"": ""500W"",
  ""Drives"": [
    ""DVD read/writer""
    /*(broken)*/,
    ""500 gigabyte hard drive"",
    ""200 gigabype hard drive""
  ]
}", sb.ToString());
        }
开发者ID:b-bot-110,项目名称:Newtonsoft.Json,代码行数:49,代码来源:WriteJsonWithJsonTextWriter.cs

示例2: Format

 private static string Format(object o)
 {
     JsonTextWriter writer = new JsonTextWriter();
     writer.ValueFormatter = new DateTimeFormatter();
     writer.WriteValue(o);
     return writer.ToString();
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:7,代码来源:TestDateTimeFormatter.cs

示例3: Format

        private static string Format(object o)
        {
            ComponentFormatter componentFormatter = new ComponentFormatter();

            CompositeFormatter compositeFormatter = new CompositeFormatter();
            compositeFormatter.AddFormatter(typeof(Car), componentFormatter);
            compositeFormatter.AddFormatter(typeof(Person), componentFormatter);

            JsonTextWriter writer = new JsonTextWriter();
            writer.ValueFormatter = compositeFormatter;

            writer.WriteValue(o);
            return writer.ToString();
        }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:14,代码来源:TestComponentFormatter.cs

示例4: ReadingAndWritingJsonText

        public void ReadingAndWritingJsonText()
        {
            #region ReadingAndWritingJsonText
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter writer = new JsonTextWriter(sw))
            {
                writer.Formatting = Formatting.Indented;

                writer.WriteStartObject();
                writer.WritePropertyName("CPU");
                writer.WriteValue("Intel");
                writer.WritePropertyName("PSU");
                writer.WriteValue("500W");
                writer.WritePropertyName("Drives");
                writer.WriteStartArray();
                writer.WriteValue("DVD read/writer");
                writer.WriteComment("(broken)");
                writer.WriteValue("500 gigabyte hard drive");
                writer.WriteValue("200 gigabype hard drive");
                writer.WriteEnd();
                writer.WriteEndObject();
            }

            // {
            //   "CPU": "Intel",
            //   "PSU": "500W",
            //   "Drives": [
            //     "DVD read/writer"
            //     /*(broken)*/,
            //     "500 gigabyte hard drive",
            //     "200 gigabype hard drive"
            //   ]
            // }
            #endregion
        }
开发者ID:ChuangYang,项目名称:Newtonsoft.Json,代码行数:37,代码来源:ReadingAndWritingJsonTests.cs

示例5: EndRequest

        public override void EndRequest()
        {
            base.EndRequest();

            Context.Response.ContentType = "application/json";
            
            using (var streamWriter = new StreamWriter(Context.Response.OutputStream))
            using (var writer = new JsonTextWriter(streamWriter))
            {
                writer.WriteStartObject();
                writer.WritePropertyName("c");
                writer.WriteValue(_channelId);
                writer.WriteEndObject();
            }
        }
开发者ID:gmt-europe,项目名称:ProtoChannel,代码行数:15,代码来源:ChannelCreateRequest.cs

示例6: ToJson

        public static string ToJson(this Person p)
        {
            StringWriter sw = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);

            // {
            writer.WriteStartObject();

            // "name" : "Jerry"
            writer.WritePropertyName("name");
            writer.WriteValue(p.Name);

            // "likes": ["Comedy", "Superman"]
            writer.WritePropertyName("likes");
            writer.WriteStartArray();
            foreach (string like in p.Likes)
            {
                writer.WriteValue(like);
            }
            writer.WriteEndArray();

            // }
            writer.WriteEndObject();

            return sw.ToString();
        }
开发者ID:925coder,项目名称:Newtonsoft.Json,代码行数:26,代码来源:PerformanceTests.cs

示例7: CustomPropertiesInternally

 public void CustomPropertiesInternally()
 {
     Point point = new Point(123, 456);
     JsonTextWriter writer = new JsonTextWriter();
     writer.WriteValue(point);
     Assert.AreEqual("{\"X\":123,\"Y\":456}", writer.ToString());
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:7,代码来源:TestComponentFormatter.cs

示例8: FormatForReading

        private static JsonReader FormatForReading(object o)
        {
            CompositeFormatter compositeFormatter = new CompositeFormatter();
            compositeFormatter.AddFormatter(typeof(Car), new ComponentFormatter());
            compositeFormatter.AddFormatter(typeof(Person), new ComponentFormatter());
            compositeFormatter.AddFormatter(typeof(OwnerCars), new ComponentFormatter());

            JsonTextWriter writer = new JsonTextWriter();
            writer.ValueFormatter = compositeFormatter;

            writer.WriteValue(o);
            return new JsonTextReader(new StringReader(writer.ToString()));
        }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:13,代码来源:TestComponentFormatter.cs

示例9: ToJsonBaseStub

            /// <summary>
            /// Give the base JSON information.  This will be used to combine with a dataset to 
            /// give the complete dataset information.
            /// 
            /// To use:
            /// ... 
            /// Write the base values
            /// writer.WriteRaw(base.ToJsonBaseStub());
            /// writer.WriteRaw(",");
            /// ...
            /// 
            /// </summary>
            /// <returns>Return a stub JSON string for the Base data.</returns>
            public string ToJsonBaseStub()
            {
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    // Start the object
                    writer.Formatting = Formatting.None;            // Make the text not indented, so not as human readable.  This will save disk writing space

                    #region Base Values

                    // NumElements
                    writer.WritePropertyName(JSON_STR_NUMELEMENTS);
                    writer.WriteValue(NumElements);

                    // ElementsMultiplier
                    writer.WritePropertyName(JSON_STR_ELEMENTSMULTIPLIER);
                    writer.WriteValue(ElementsMultiplier);

                    #endregion

                }

                return sb.ToString();
            }
开发者ID:Bobfrat,项目名称:RTI,代码行数:38,代码来源:BaseDataSet.cs

示例10: ProcessRequest

        protected override void ProcessRequest()
        {
            if (!Modified())
            {
                Response.StatusCode = 304;
                return;
            }

            //
            // Generate the SMD object graph.
            //

            IRpcServiceDescriptor service = TargetService.GetDescriptor();

            JObject smd = new JObject();
            
            smd.Put("SMDVersion", ".1");
            smd.Put("objectName", JsonRpcServices.GetServiceName(TargetService));
            smd.Put("serviceType", "JSON-RPC");
            smd.Put("serviceURL", Request.FilePath); // TODO: Check whether this should be an absolute path from the protocol root.

            IRpcMethodDescriptor[] methods = service.GetMethods();

            if (methods.Length > 0) // TODO: Check if methods entry can be skipped if there are none.
            {
                JArray smdMethods = new JArray();

                foreach (IRpcMethodDescriptor method in methods)
                {
                    JObject smdMethod = new JObject();
                    smdMethod.Put("name", method.Name);
                
                    IRpcParameterDescriptor[] parameters = method.GetParameters();

                    if (parameters.Length > 0) // TODO: Check if parameters entry can be skipped if there are none.
                    {
                        JArray smdParameters = new JArray();

                        foreach (IRpcParameterDescriptor parameter in parameters)
                        {
                            JObject smdParameter = new JObject();
                            smdParameter.Put("name", parameter.Name);
                            smdParameters.Add(smdParameter);
                        }

                        smdMethod.Put("parameters", smdParameters);
                    }

                    smdMethods.Add(smdMethod);
                }

                smd.Put("methods", smdMethods);
            }

            //
            // Generate the response.
            //

            if (HasLastModifiedTime)
            {
                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.Cache.SetLastModified(LastModifiedTime);
            }

            Response.ContentType = "text/plain";

            Response.AppendHeader("Content-Disposition", 
                "attachment; filename=" + service.Name + ".smd");

            JsonTextWriter writer = new JsonTextWriter(Response.Output);
            writer.WriteValue(smd);
        }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:72,代码来源:JsonRpcSimpleMethodDescription.cs

示例11: SerializeMessage

        public static void SerializeMessage(JsonTextWriter writer, ServiceType type, object message)
        {
            Require.NotNull(writer, "writer");
            Require.NotNull(type, "type");
            Require.NotNull(message, "message");

            writer.WriteStartObject();

            foreach (var field in type.Fields.Values)
            {
                if (
                    field.ShouldSerializeMethod != null &&
                    !field.ShouldSerializeMethod(message)
                )
                    continue;

                writer.WritePropertyName(field.Tag.ToString(CultureInfo.InvariantCulture));

                object value = field.Getter(message);

                if (value == null)
                {
                    writer.WriteNull();
                }
                else if (field.CollectionType != null)
                {
                    writer.WriteStartArray();

                    foreach (object item in (IEnumerable)value)
                    {
                        if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
                            SerializeMessage(writer, field.ServiceType, item);
                        else
                            writer.WriteValue(item);
                    }

                    writer.WriteEndArray();
                }
                else 
                {
                    if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
                        SerializeMessage(writer, field.ServiceType, value);
                    else
                        writer.WriteValue(value);
                }
            }

            writer.WriteEndObject();
        }
开发者ID:gmt-europe,项目名称:ProtoChannel,代码行数:49,代码来源:JsonUtil.cs

示例12: Export

 private static string Export(object o)
 {
     JsonTextWriter writer = new JsonTextWriter();
     writer.WriteValue(o);
     return writer.ToString();
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:6,代码来源:TestNameValueCollectionExporter.cs

示例13: Format

        private static string Format(object o)
        {
            JsonTextWriter writer = new JsonTextWriter();
            writer.ValueFormatter = new NameValueCollectionFormatter();

            writer.WriteValue(o);
            return writer.ToString();
        }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:8,代码来源:TestNameValueCollectionFormatter.cs

示例14: BuildResponseJson

        private void BuildResponseJson(StringBuilder sb, PendingDownstreamMessage message)
        {
            using (var stringWriter = new StringWriter(sb))
            using (var writer = new JsonTextWriter(stringWriter))
            {
                if (message == null)
                {
                    writer.WriteStartArray();
                    writer.WriteValue("noop");
                    writer.WriteEndArray();
                }
                else
                {
                    var responseType = message.Message.GetType();
                    var responseServiceType = ServiceRegistry.GetAssembly(responseType.Assembly).TypesByType[responseType];

                    writer.WriteStartObject();
                    writer.WritePropertyName("r");
                    writer.WriteValue((int)message.Kind);
                    writer.WritePropertyName("a");
                    writer.WriteValue((int)message.AssociationId);
                    writer.WritePropertyName("t");
                    writer.WriteValue(responseServiceType.Message.Id);
                    writer.WritePropertyName("p");

                    JsonUtil.SerializeMessage(writer, responseServiceType, message.Message);

                    writer.WriteEndObject();
                }
            }
        }
开发者ID:gmt-europe,项目名称:ProtoChannel,代码行数:31,代码来源:ChannelDownstreamRequest.cs


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