Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/ibm/json/java/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ static public JSONObject parse(InputStream is) throws IOException {
return parse(isr);
}

/**
* Convert a String of JSON text into object form.
* @param str The JSON string to parse into a Java Object.
* @param duplicateProtection Whether having duplicate keys in an object will throw an error
* @return The contructed JSON Object.
*
* @throws IOEXception Thrown if malformed JSON is read,
*/
static public JSONObject parse(String str, boolean duplicateProtection) throws IOException {
StringReader strReader = new StringReader(str);
return new Parser(strReader, duplicateProtection).parse();
}


/**
* Create a new instance of this class.
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/ibm/json/java/OrderedJSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ static public JSONObject parse(InputStream is) throws IOException {
return parse(isr);
}

/**
* Convert a String of JSON text into object form.
* @param str The JSON string to parse into a Java Object.
* @param duplicateProtection Whether having duplicate keys in an object will throw an error
* @return The contructed JSON Object. Note that the JSONObject will be an instance of OrderedJSONObject and as such, attribute order is maintained.
*
* @throws IOEXception Thrown if malformed JSON is read,
*/
static public JSONObject parse(String str, boolean duplicateProtection) throws IOException {
StringReader strReader = new StringReader(str);
return new Parser(strReader, duplicateProtection).parse(true);
}

/**
* Method to put a JSON'able object into the instance. Note that the order of initial puts controls the order of serialization.
* Meaning that the first time an item is put into the object determines is position of serialization. Subsequent puts with the same
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/ibm/json/java/internal/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Parser

private Tokenizer tokenizer;
private Token lastToken;
private boolean duplicateProtection;

/**
* Contructor
Expand All @@ -39,6 +40,11 @@ public Parser(Reader reader) throws IOException {
this.tokenizer = new Tokenizer(reader);
}

public Parser(Reader reader, boolean duplicateProtection) throws IOException {
this.tokenizer = new Tokenizer(reader);
this.duplicateProtection = duplicateProtection;
}

/**
* Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within.
* Same as calling parse(false);
Expand Down Expand Up @@ -111,6 +117,11 @@ public JSONObject parseObject(boolean ordered) throws IOException {
lastToken = tokenizer.next();
Object val = parseValue(ordered);

if (duplicateProtection) {
if (result.get(key) != null) {
throw new IOException("Duplicate Key \'" + key + "\'");
}
}
result.put(key, val);

if (lastToken == Token.TokenComma)
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/ibm/json4j/DuplicateTagTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ibm.json4j;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.junit.Test;

import com.ibm.json.java.JSONObject;

public class DuplicateTagTest {

@Test
public void testDuplicateId() throws IOException {
String inputJson = "{\"duplicate\": 123, \"duplicate\": 456}";

try {
JSONObject obj = JSONObject.parse(inputJson, true);
fail("Exception should have been thrown");
} catch (IOException e) {
assertEquals("Duplicate Key \'duplicate\'", e.getMessage());
}
}

}
Loading