Skip to content

Commit 01197bd

Browse files
authored
Added HasKey and GetValueOrDefault
1 parent 998edec commit 01197bd

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

SimpleJSON.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
* It mainly has been written as a simple JSON parser. It can build a JSON string
66
* from the node-tree, or generate a node tree from any valid JSON string.
77
*
8-
* If you want to use compression when saving to file / stream / B64 you have to include
9-
* SharpZipLib ( http://www.icsharpcode.net/opensource/sharpziplib/ ) in your project and
10-
* define "USE_SharpZipLib" at the top of the file
11-
*
128
* Written by Bunny83
139
* 2012-06-09
1410
*
@@ -92,6 +88,10 @@
9288
*
9389
* [2018-04-25 Update]
9490
* - Added support for parsing single values (JSONBool, JSONString, JSONNumber, JSONNull) as top level value.
91+
*
92+
* [2019-02-18 Update]
93+
* - Added HasKey(key) and GetValueOrDefault(key, default) to the JSONNode class to provide way to read
94+
* values conditionally without creating a LazyCreator
9595
*
9696
* The MIT License (MIT)
9797
*
@@ -308,6 +308,16 @@ public IEnumerable<JSONNode> DeepChildren
308308
}
309309
}
310310

311+
public virtual bool HasKey(string aKey)
312+
{
313+
return false;
314+
}
315+
316+
public virtual JSONNode GetValueOrDefault(string aKey, JSONNode aDefault)
317+
{
318+
return aDefault;
319+
}
320+
311321
public override string ToString()
312322
{
313323
StringBuilder sb = new StringBuilder();
@@ -950,6 +960,19 @@ public override JSONNode Remove(JSONNode aNode)
950960
}
951961
}
952962

963+
public override bool HasKey(string aKey)
964+
{
965+
return m_Dict.ContainsKey(aKey);
966+
}
967+
968+
public override JSONNode GetValueOrDefault(string aKey, JSONNode aDefault)
969+
{
970+
JSONNode res;
971+
if (m_Dict.TryGetValue(aKey, out res))
972+
return res;
973+
return aDefault;
974+
}
975+
953976
public override IEnumerable<JSONNode> Children
954977
{
955978
get

0 commit comments

Comments
 (0)