-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNewtonsoftExtensions
45 lines (43 loc) · 1.17 KB
/
NewtonsoftExtensions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using System;
public static class NewtonsoftExtensions
{
public static bool IsValidJson(string json)
{
json = json.Trim();
if ((json.StartsWith("{") && json.EndsWith("}")) || //For object
(json.StartsWith("[") && json.EndsWith("]"))) //For array
{
try
{
JToken.Parse(json);
return true;
}
catch (JsonReaderException jex)
{
//Exception in parsing json
Debug.Log(jex.Message);
return false;
}
catch (Exception ex) //some other exception
{
Debug.Log(ex.ToString());
return false;
}
}
else
{
return false;
}
}
public static void Rename(this JToken token, string newName)
{
var parent = token.Parent;
if (parent == null)
throw new InvalidOperationException("The parent is missing.");
var newToken = new JProperty(newName, token);
parent.Replace(newToken);
}
}