File tree Expand file tree Collapse file tree 4 files changed +63
-0
lines changed
main/java/com/ibm/json/java Expand file tree Collapse file tree 4 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,19 @@ static public JSONObject parse(InputStream is) throws IOException {
113113 return parse (isr );
114114 }
115115
116+ /**
117+ * Convert a String of JSON text into object form.
118+ * @param str The JSON string to parse into a Java Object.
119+ * @param duplicateProtection Whether having duplicate keys in an object will throw an error
120+ * @return The contructed JSON Object.
121+ *
122+ * @throws IOEXception Thrown if malformed JSON is read,
123+ */
124+ static public JSONObject parse (String str , boolean duplicateProtection ) throws IOException {
125+ StringReader strReader = new StringReader (str );
126+ return new Parser (strReader , duplicateProtection ).parse ();
127+ }
128+
116129
117130 /**
118131 * Create a new instance of this class.
Original file line number Diff line number Diff line change @@ -99,6 +99,19 @@ static public JSONObject parse(InputStream is) throws IOException {
9999 return parse (isr );
100100 }
101101
102+ /**
103+ * Convert a String of JSON text into object form.
104+ * @param str The JSON string to parse into a Java Object.
105+ * @param duplicateProtection Whether having duplicate keys in an object will throw an error
106+ * @return The contructed JSON Object. Note that the JSONObject will be an instance of OrderedJSONObject and as such, attribute order is maintained.
107+ *
108+ * @throws IOEXception Thrown if malformed JSON is read,
109+ */
110+ static public JSONObject parse (String str , boolean duplicateProtection ) throws IOException {
111+ StringReader strReader = new StringReader (str );
112+ return new Parser (strReader , duplicateProtection ).parse (true );
113+ }
114+
102115 /**
103116 * Method to put a JSON'able object into the instance. Note that the order of initial puts controls the order of serialization.
104117 * Meaning that the first time an item is put into the object determines is position of serialization. Subsequent puts with the same
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ public class Parser
2626
2727 private Tokenizer tokenizer ;
2828 private Token lastToken ;
29+ private boolean duplicateProtection ;
2930
3031 /**
3132 * Contructor
@@ -39,6 +40,11 @@ public Parser(Reader reader) throws IOException {
3940 this .tokenizer = new Tokenizer (reader );
4041 }
4142
43+ public Parser (Reader reader , boolean duplicateProtection ) throws IOException {
44+ this .tokenizer = new Tokenizer (reader );
45+ this .duplicateProtection = duplicateProtection ;
46+ }
47+
4248 /**
4349 * Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within.
4450 * Same as calling parse(false);
@@ -111,6 +117,11 @@ public JSONObject parseObject(boolean ordered) throws IOException {
111117 lastToken = tokenizer .next ();
112118 Object val = parseValue (ordered );
113119
120+ if (duplicateProtection ) {
121+ if (result .get (key ) != null ) {
122+ throw new IOException ("Duplicate Key \' " + key + "\' " );
123+ }
124+ }
114125 result .put (key , val );
115126
116127 if (lastToken == Token .TokenComma )
Original file line number Diff line number Diff line change 1+ package com .ibm .json4j ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .fail ;
5+
6+ import java .io .IOException ;
7+
8+ import org .junit .Test ;
9+
10+ import com .ibm .json .java .JSONObject ;
11+
12+ public class DuplicateTagTest {
13+
14+ @ Test
15+ public void testDuplicateId () throws IOException {
16+ String inputJson = "{\" duplicate\" : 123, \" duplicate\" : 456}" ;
17+
18+ try {
19+ JSONObject obj = JSONObject .parse (inputJson , true );
20+ fail ("Exception should have been thrown" );
21+ } catch (IOException e ) {
22+ assertEquals ("Duplicate Key \' duplicate\' " , e .getMessage ());
23+ }
24+ }
25+
26+ }
You can’t perform that action at this time.
0 commit comments