Extend JSONObject with utility methods

master
Harald Wolff 2019-08-19 14:13:59 +02:00
parent a0a475c743
commit 0140149fe0
1 changed files with 20 additions and 2 deletions

View File

@ -3,8 +3,9 @@ using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Linq; using System.Linq;
using ln.types.btree;
using System.Security.Cryptography; using System.Security.Cryptography;
using ln.types.btree;
using ln.json.mapping;
namespace ln.json namespace ln.json
{ {
@ -28,17 +29,27 @@ namespace ln.json
set => values[property] = value; set => values[property] = value;
} }
public JSONObject Add(string propertyName,JSONValue value) public JSONObject Add(string propertyName, JSONValue value)
{ {
values[propertyName] = value; values[propertyName] = value;
return this; return this;
} }
public JSONObject Add(string propertyName, object value)
{
values[propertyName] = JSONMapper.DefaultMapper.ToJson(value);
return this;
}
public bool ContainsKey(string key) public bool ContainsKey(string key)
{ {
return values.ContainsKey(key); return values.ContainsKey(key);
} }
public T ToObject<T>()
{
return JSONMapper.DefaultMapper.FromJson<T>(this);
}
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -63,5 +74,12 @@ namespace ln.json
return sb.ToString(); return sb.ToString();
} }
public static JSONObject From(object value)
{
return (JSONObject)JSONMapper.DefaultMapper.ToJson(value);
}
} }
} }