Moving ConvertToJson to ObjectDumper

This commit is contained in:
Stanley Goldman
2014-10-22 20:51:26 -04:00
parent d1f0eda6f4
commit 43bcded513
2 changed files with 50 additions and 48 deletions

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Xml.Linq;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
@@ -294,5 +296,52 @@ namespace Orchard.DesignerTools.Services {
SaveCurrentNode();
_current.Add(_current = new XElement(tag));
}
public static void ConvertToJSon(XElement x, StringBuilder sb) {
if (x == null) {
return;
}
switch (x.Name.ToString()) {
case "ul":
var first = true;
foreach (var li in x.Elements()) {
if (!first) sb.Append(",");
ConvertToJSon(li, sb);
first = false;
}
break;
case "li":
var name = x.Element("h1").Value;
var value = x.Element("span").Value;
sb.AppendFormat("\"name\": \"{0}\", ", FormatJsonValue(name));
sb.AppendFormat("\"value\": \"{0}\"", FormatJsonValue(value));
var ul = x.Element("ul");
if (ul != null && ul.Descendants().Any()) {
sb.Append(", \"children\": [");
first = true;
foreach (var li in ul.Elements()) {
sb.Append(first ? "{ " : ", {");
ConvertToJSon(li, sb);
sb.Append(" }");
first = false;
}
sb.Append("]");
}
break;
}
}
public static string FormatJsonValue(string value) {
if (String.IsNullOrEmpty(value)) {
return String.Empty;
}
// replace " by \" in json strings
return HttpUtility.HtmlEncode(value).Replace(@"\", @"\\").Replace("\"", @"\""").Replace("\r\n", @"\n").Replace("\r", @"\n").Replace("\n", @"\n");
}
}
}

View File

@@ -112,7 +112,7 @@ namespace Orchard.DesignerTools.Services {
var dump = new ObjectDumper(6).Dump(context.Shape, "Model");
var sb = new StringBuilder();
ConvertToJSon(dump, sb);
ObjectDumper.ConvertToJSon(dump, sb);
shape._Dump = sb.ToString();
shape.Template = null;
@@ -165,53 +165,6 @@ namespace Orchard.DesignerTools.Services {
public void Displayed(ShapeDisplayedContext context) {
}
public static void ConvertToJSon(XElement x, StringBuilder sb) {
if(x == null) {
return;
}
switch (x.Name.ToString()) {
case "ul" :
var first = true;
foreach(var li in x.Elements()) {
if (!first) sb.Append(",");
ConvertToJSon(li, sb);
first = false;
}
break;
case "li":
var name = x.Element("h1").Value;
var value = x.Element("span").Value;
sb.AppendFormat("\"name\": \"{0}\", ", FormatJsonValue(name));
sb.AppendFormat("\"value\": \"{0}\"", FormatJsonValue(value));
var ul = x.Element("ul");
if (ul != null && ul.Descendants().Any()) {
sb.Append(", \"children\": [");
first = true;
foreach (var li in ul.Elements()) {
sb.Append(first ? "{ " : ", {");
ConvertToJSon(li, sb);
sb.Append(" }");
first = false;
}
sb.Append("]");
}
break;
}
}
public static string FormatJsonValue(string value) {
if(String.IsNullOrEmpty(value)) {
return String.Empty;
}
// replace " by \" in json strings
return HttpUtility.HtmlEncode(value).Replace(@"\", @"\\").Replace("\"", @"\""").Replace("\r\n", @"\n").Replace("\r", @"\n").Replace("\n", @"\n");
}
private static string FormatShapeFilename(string shape, string shapeType, string displayType, string themePrefix, string extension) {
if (!String.IsNullOrWhiteSpace(displayType)) {