當前位置: 首頁>>代碼示例>>C#>>正文


C# JsonTextWriter.WritePropertyString方法代碼示例

本文整理匯總了C#中Newtonsoft.Json.JsonTextWriter.WritePropertyString方法的典型用法代碼示例。如果您正苦於以下問題:C# JsonTextWriter.WritePropertyString方法的具體用法?C# JsonTextWriter.WritePropertyString怎麽用?C# JsonTextWriter.WritePropertyString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Newtonsoft.Json.JsonTextWriter的用法示例。


在下文中一共展示了JsonTextWriter.WritePropertyString方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: WriteAsJson

        public static void WriteAsJson([NotNull] this Item item, [NotNull] TextWriter writer, [CanBeNull] Action<TextWriter> writeInner = null)
        {
            var output = new JsonTextWriter(writer)
            {
                Formatting = Formatting.Indented
            };

            output.WriteStartObject();

            output.WriteStartObject("Item");
            output.WritePropertyString("Name", item.ItemName);
            output.WritePropertyStringIf("Id", item.Uri.Guid.Format());
            output.WritePropertyStringIf("Database", item.DatabaseName);
            output.WritePropertyStringIf("Path", item.ItemIdOrPath);
            output.WritePropertyStringIf("Template", item.TemplateIdOrPath);

            output.WriteStartObject("Fields");

            var sharedFields = item.Fields.Where(f => string.IsNullOrEmpty(f.Language) && f.Version == 0).ToList();
            var unversionedFields = item.Fields.Where(f => !string.IsNullOrEmpty(f.Language) && f.Version == 0).ToList();
            var versionedFields = item.Fields.Where(f => !string.IsNullOrEmpty(f.Language) && f.Version != 0).ToList();

            foreach (var field in sharedFields)
            {
                if (string.Equals(field.TemplateField.Type, "Layout", StringComparison.OrdinalIgnoreCase) || string.Equals(field.FieldName, "__Renderings", StringComparison.OrdinalIgnoreCase) || string.Equals(field.FieldName, "__Final Renderings", StringComparison.OrdinalIgnoreCase))
                {
                    var layout = "{" + field.Value + "}";
                    var jToken = layout.ToJToken();
                    jToken?.First.WriteTo(output);
                    continue;
                }

                output.WritePropertyString(field.FieldName, field.Value);
            }

            if (unversionedFields.Any())
            {
                output.WriteStartObject("Unversioned");

                foreach (var language in unversionedFields.Select(f => f.Language).Distinct())
                {
                    output.WriteStartObject(language);

                    foreach (var field in unversionedFields.Where(f => f.Language == language))
                    {
                        output.WritePropertyString(field.FieldName, field.Value);
                    }

                    output.WriteEndObject();
                }

                output.WriteEndObject();
            }

            if (versionedFields.Any())
            {
                output.WriteStartObject("Versioned");

                foreach (var language in versionedFields.Select(f => f.Language).Distinct())
                {
                    output.WriteStartObject(language);

                    foreach (var version in versionedFields.Where(f => f.Language == language).Select(f => f.Version).Distinct())
                    {
                        output.WriteStartObject(version.ToString());

                        foreach (var field in versionedFields.Where(f => f.Language == language && f.Version == version))
                        {
                            output.WritePropertyString(field.FieldName, field.Value);
                        }

                        output.WriteEndObject();
                    }

                    output.WriteEndObject();
                }

                output.WriteEndObject();
            }

            output.WriteEndObject();

            if (writeInner != null)
            {
                writeInner(writer);
            }

            output.WriteEndObject();

            output.WriteEndObject();
        }
開發者ID:pveller,項目名稱:Sitecore.Pathfinder,代碼行數:91,代碼來源:FormatExtensions.cs


注:本文中的Newtonsoft.Json.JsonTextWriter.WritePropertyString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。