Skip to content

Commit dc03614

Browse files
committed
♻️ GQL Json Content support added
1 parent 0afe285 commit dc03614

12 files changed

Lines changed: 249 additions & 139 deletions

File tree

Contentstack.Utils/Converters/NodeJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public override Node ReadJson(JsonReader reader, Type objectType, Node existingV
1414
if (jObject["type"] == null)
1515
{
1616
node = new TextNode();
17-
node.type = Enums.NodeType.Text;
17+
node.type = "text";
1818
}else
1919
{
2020
node = new Node();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
6+
7+
namespace Contentstack.Utils.Converters
8+
{
9+
public class RTEJsonConverter : JsonConverter
10+
{
11+
public override bool CanConvert(Type objectType)
12+
{
13+
throw new NotImplementedException();
14+
}
15+
16+
public override object ReadJson(JsonReader reader, Type objectType,
17+
object existingValue, JsonSerializer serializer)
18+
{
19+
JObject jo = JObject.Load(reader);
20+
object targetObj = Activator.CreateInstance(objectType);
21+
22+
foreach (PropertyInfo prop in objectType.GetProperties()
23+
.Where(p => p.CanRead && p.CanWrite))
24+
{
25+
JsonPropertyAttribute att = prop.GetCustomAttributes(true)
26+
.OfType<JsonPropertyAttribute>()
27+
.FirstOrDefault();
28+
29+
string jsonPath = (att != null ? att.PropertyName : prop.Name);
30+
JToken token = jo.SelectToken(jsonPath);
31+
32+
if (token != null && token.Type != JTokenType.Null)
33+
{
34+
object value = token.ToObject(prop.PropertyType, serializer);
35+
prop.SetValue(targetObj, value, null);
36+
}
37+
}
38+
39+
return targetObj;
40+
}
41+
42+
43+
public override void WriteJson(JsonWriter writer, object value,
44+
JsonSerializer serializer)
45+
{
46+
47+
}
48+
}
49+
}

Contentstack.Utils/Enums/NodeType.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Contentstack.Utils.Interfaces
5+
{
6+
public class IEdges<T> where T: IEmbeddedObject
7+
{
8+
[JsonProperty("node")]
9+
public T Node
10+
{
11+
get;
12+
set;
13+
}
14+
}
15+
}
16+
17+

Contentstack.Utils/Interfaces/IOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public interface IRenderable
1111
{
1212
string RenderOption(IEmbeddedObject entry, Metadata metadata);
1313
string RenderMark(MarkType markType, string text);
14-
string RenderNode(NodeType nodeType, Node node, NodeChildrenCallBack callBack);
14+
string RenderNode(string nodeType, Node node, NodeChildrenCallBack callBack);
1515
}
1616
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Contentstack.Utils.Converters;
4+
using Contentstack.Utils.Interfaces;
5+
using Newtonsoft.Json;
6+
7+
namespace Contentstack.Utils.Models
8+
{
9+
[Newtonsoft.Json.JsonConverter(typeof(RTEJsonConverter))]
10+
public class JsonRTENode<T> where T: IEmbeddedObject
11+
{
12+
[JsonProperty("json")]
13+
public Node Json { get; set; }
14+
[JsonProperty("embedded_itemsConnection.edges")]
15+
public List<IEdges<T>> Edges { get; set; }
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Contentstack.Utils.Converters;
4+
using Contentstack.Utils.Interfaces;
5+
using Newtonsoft.Json;
6+
7+
namespace Contentstack.Utils.Models
8+
{
9+
[Newtonsoft.Json.JsonConverter(typeof(RTEJsonConverter))]
10+
public class JsonRTENodes<T> where T : IEmbeddedObject
11+
{
12+
[JsonProperty("json")]
13+
public List<Node> Json { get; set; }
14+
[JsonProperty("embedded_itemsConnection.edges")]
15+
public List<IEdges<T>> Edges { get; set; }
16+
}
17+
}

Contentstack.Utils/Models/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Contentstack.Utils.Models
1010
[JsonConverter(typeof(NodeJsonConverter))]
1111
public class Node
1212
{
13-
public NodeType type { get; set; }
13+
public string type { get; set; }
1414

1515
public IDictionary<string, object> attrs { get; set; }
1616

Contentstack.Utils/Models/Options.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Contentstack.Utils.Enums;
1+
using System;
2+
using Contentstack.Utils.Enums;
23
using Contentstack.Utils.Interfaces;
34

45
namespace Contentstack.Utils.Models
@@ -85,69 +86,68 @@ public virtual string RenderMark(MarkType markType, string text)
8586
return text;
8687
}
8788

88-
public virtual string RenderNode(NodeType nodeType, Node node, NodeChildrenCallBack callBack)
89+
public virtual string RenderNode(string nodeType, Node node, NodeChildrenCallBack callBack)
8990
{
9091
string href = "";
91-
9292
switch (nodeType)
9393
{
94-
case NodeType.Paragraph:
94+
case "p":
9595
return $"<p>{callBack(node.children)}</p>";
96-
case NodeType.Link:
96+
case "a":
9797
if (node.attrs.ContainsKey("url"))
9898
{
9999
href = (string)node.attrs["url"];
100100
}
101101
return $"<a href=\"{href}\">{callBack(node.children)}</a>";
102-
case NodeType.Image:
102+
case "img":
103103
if (node.attrs.ContainsKey("url"))
104104
{
105105
href = (string)node.attrs["url"];
106106
}
107107
return $"<img src=\"{href}\" />{callBack(node.children)}";
108-
case NodeType.Embed:
108+
case "embed":
109109
if (node.attrs.ContainsKey("url"))
110110
{
111111
href = (string)node.attrs["url"];
112112
}
113113
return $"<iframe src=\"{href}\">{callBack(node.children)}</iframe>";
114-
case NodeType.Heading_1:
114+
case "h1":
115115
return $"<h1>{callBack(node.children)}</h1>";
116-
case NodeType.Heading_2:
116+
case "h2":
117117
return $"<h2>{callBack(node.children)}</h2>";
118-
case NodeType.Heading_3:
118+
case "h3":
119119
return $"<h3>{callBack(node.children)}</h3>";
120-
case NodeType.Heading_4:
120+
case "h4":
121121
return $"<h4>{callBack(node.children)}</h4>";
122-
case NodeType.Heading_5:
122+
case "h5":
123123
return $"<h5>{callBack(node.children)}</h5>";
124-
case NodeType.Heading_6:
124+
case "h6":
125125
return $"<h6>{callBack(node.children)}</h6>";
126-
case NodeType.OrderList:
126+
case "ol":
127127
return $"<ol>{callBack(node.children)}</ol>";
128-
case NodeType.UnOrderList:
128+
case "ul":
129129
return $"<ul>{callBack(node.children)}</ul>";
130-
case NodeType.ListItem:
130+
case "li":
131131
return $"<li>{callBack(node.children)}</li>";
132-
case NodeType.Hr:
132+
case "hr":
133133
return $"<hr>";
134-
case NodeType.Table:
134+
case "table":
135135
return $"<table>{callBack(node.children)}</table>";
136-
case NodeType.TableHeader:
136+
case "thead":
137137
return $"<thead>{callBack(node.children)}</thead>";
138-
case NodeType.TableBody:
138+
case "tbody":
139139
return $"<tbody>{callBack(node.children)}</tbody>";
140-
case NodeType.TableFooter:
140+
case "tfoot":
141141
return $"<tfoot>{callBack(node.children)}</tfoot>";
142-
case NodeType.TableRow:
142+
case "tr":
143143
return $"<tr>{callBack(node.children)}</tr>";
144-
case NodeType.TableHead:
144+
case "th":
145145
return $"<th>{callBack(node.children)}</th>";
146-
case NodeType.TableData:
146+
case "td":
147147
return $"<td>{callBack(node.children)}</td>";
148-
case NodeType.BlockQuote:
148+
case "blockquote":
149149
return $"<blockquote>{callBack(node.children)}</blockquote>";
150-
case NodeType.Code:
150+
case "code":
151151
return $"<code>{callBack(node.children)}</code>";
152152
default:
153153
return callBack(node.children);

0 commit comments

Comments
 (0)