Skip to content

Instantly share code, notes, and snippets.

@Y-Koji
Last active August 16, 2020 11:08
Show Gist options
  • Save Y-Koji/8ae803e061a1447a1a542816338e03aa to your computer and use it in GitHub Desktop.
Save Y-Koji/8ae803e061a1447a1a542816338e03aa to your computer and use it in GitHub Desktop.
Linq to Json

Linq to Json (Simple)

.NET標準実装のLinq to XMLをJsonで利用できるようにするクラス群です。

Json生成

Json解析

using System.Xml.Linq;
namespace Json.Linq
{
public class JDocument : XDocument
{
public JDocument() : base() { }
public JDocument(params object[] content) : base(content) { }
public JDocument(XDocument other) : base(other) { }
public JDocument(XDeclaration declaration, params object[] content) : base(declaration, content) { }
public static new JDocument Parse(string json) => json;
public static implicit operator JDocument(string json)
{
XDocument xd = JDocumentExtensions.Load(json);
JDocument jd = new JDocument();
jd.Add(xd.Element("root"));
return jd;
}
public override string ToString()
{
return this.ToJsonString(true);
}
}
}
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace Json.Linq
{
public static class JDocumentExtensions
{
public static XDocument Load(Stream stream)
{
using (XmlReader xr = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max))
{
return XDocument.Load(xr);
}
}
public static XDocument Load(string json)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
return Load(ms);
}
}
public static XDocument LoadFromFile(string filePath)
{
using (FileStream fs = File.OpenRead(filePath))
{
return Load(fs);
}
}
public static void SaveAsJson(this XDocument self, Stream stream, bool isFormatted = false)
{
using (XmlWriter xw = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, true, isFormatted, " "))
{
self.WriteTo(xw);
xw.Flush();
stream.Flush();
}
}
public static string ToJsonString(this XDocument self, bool isFormatted = false)
{
using (MemoryStream ms = new MemoryStream())
{
self.SaveAsJson(ms, isFormatted);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
public static string ToXmlString(this XDocument self, bool isFormatted = false)
{
using (MemoryStream ms = new MemoryStream())
using (XmlWriter xw = XmlWriter.Create(ms, new XmlWriterSettings { Indent = isFormatted, IndentChars = " " }))
{
self.WriteTo(xw);
xw.Flush();
ms.Flush();
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Json.Linq
{
public class JElement : XElement
{
public JElement(XName name) : base(name) { }
public JElement(XElement other) : base(other) { }
public JElement(XStreamingElement other) : base(other) { }
public JElement(XName name, object content) : base(name)
{
switch (content)
{
case int x:
SetAttributeValue("type", "number");
Value = x.ToString();
break;
case bool b:
SetAttributeValue("type", "boolean");
Value = b ? "true" : "false";
break;
case string s:
SetAttributeValue("type", "string");
Value = s;
break;
case IEnumerable array:
SetAttributeValue("type", "array");
foreach (var item in array)
{
if (item is JElement element)
{
Add(element);
continue;
}
Add(new JElement("item", item));
}
break;
case JElement e:
Add(e);
break;
}
}
public JElement(XName name, params object[] content) : base(name, content)
{
if (null == content)
{
SetAttributeValue("type", "null");
}
else
{
SetAttributeValue("type", "object");
}
}
public static new IEnumerable<JElement> Parse(string json)
{
return ((JElement)json).Elements().Select(x => new JElement(x));
}
public override string ToString()
{
JDocument jd = "{}";
JElement element = new JElement(this);
jd.Root.Add(element);
return jd.ToJsonString();
}
public static implicit operator JElement(string json)
{
JDocument jd = json;
JElement je = new JElement(jd.Root);
je.Name = "item";
return je;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment