-
-
Notifications
You must be signed in to change notification settings - Fork 76
fix Issue 177 (support incomplet json messages) #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
UrielCh
wants to merge
6
commits into
master
Choose a base branch
from
ISSUE_177
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f70cd9e
Add support for incomplete JSON parsing and enhance documentation
UrielCh eebc79a
Enhance test coverage for JSONArray, JSONObject, JSONUtil, and JSONValue
UrielCh 668c0a8
Add JSONParser.ACCEPT_INCOMPLETE to support parsing of partial JSON i…
UrielCh 38b7ec5
add a precommit script
UrielCh be5a5bf
RENAME acceptIncomplet to acceptIncomplete
UrielCh 6d28723
rename MODE_PERMISSIVE_NEW to MODE_PERMISSIVE_WITH_INCOMPLETE
UrielCh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,7 @@ | |
* @author FangYidong <[email protected]> | ||
* @author Uriel Chemouni <[email protected]> | ||
*/ | ||
public class JSONArray extends ArrayList<Object> | ||
implements List<Object>, JSONAwareEx, JSONStreamAwareEx { | ||
public class JSONArray extends ArrayList<Object> implements JSONAwareEx, JSONStreamAwareEx { | ||
private static final long serialVersionUID = 9106884089231309568L; | ||
|
||
public JSONArray() {} | ||
|
@@ -76,13 +75,21 @@ public static void writeJSONString( | |
JsonWriter.JSONIterableWriter.writeJSONString(list, out, compression); | ||
} | ||
|
||
/** | ||
* Encode a list into JSON text and write it to out. If this list is also a JSONStreamAware or a | ||
* JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level. | ||
* | ||
* @param list | ||
* @param out | ||
* @throws IOException | ||
*/ | ||
public static void writeJSONString(List<? extends Object> list, Appendable out) | ||
throws IOException { | ||
writeJSONString(list, out, JSONValue.COMPRESSION); | ||
} | ||
|
||
/** | ||
* Appends the specified element and returns this. Handy alternative to add(E e) method. | ||
* Appends the specified element and returns this. same effect that add(E e) method. | ||
* | ||
* @param element element to be appended to this array. | ||
* @return this | ||
|
@@ -92,6 +99,11 @@ public JSONArray appendElement(Object element) { | |
return this; | ||
} | ||
|
||
/** | ||
* Merges the specified object into this array. can trigger an add(E e) or addAll(E e) method. | ||
* | ||
* @param o2 | ||
*/ | ||
public void merge(Object o2) { | ||
JSONObject.merge(this, o2); | ||
} | ||
|
@@ -119,10 +131,21 @@ public String toString(JSONStyle compression) { | |
return toJSONString(compression); | ||
} | ||
|
||
/** | ||
* JSONStreamAwareEx interface | ||
* | ||
* @param out output stream | ||
*/ | ||
public void writeJSONString(Appendable out) throws IOException { | ||
writeJSONString(this, out, JSONValue.COMPRESSION); | ||
} | ||
|
||
/** | ||
* JSONStreamAwareEx interface | ||
* | ||
* @param out output stream | ||
* @param compression compression param | ||
*/ | ||
public void writeJSONString(Appendable out, JSONStyle compression) throws IOException { | ||
writeJSONString(this, out, compression); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
json-smart/src/test/java/net/minidev/json/test/JSONIncompletModeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package net.minidev.json.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import net.minidev.json.JSONArray; | ||
import net.minidev.json.JSONObject; | ||
import net.minidev.json.parser.JSONParser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** TODO make the same tests in stream and bytes mode */ | ||
public class JSONIncompletModeTest { | ||
|
||
@Test | ||
public void testArraySimple() throws Exception { | ||
String s = "[1"; | ||
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE | JSONParser.ACCEPT_INCOMPLETE); | ||
JSONArray array = (JSONArray) p.parse(s); | ||
assertEquals(Long.valueOf(1), (Long) array.get(0)); | ||
} | ||
|
||
@Test | ||
public void testArrayInObject1() throws Exception { | ||
String s = "{\"obj\":[1"; | ||
String result = "{\"obj\":[1]}"; | ||
|
||
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE | JSONParser.ACCEPT_INCOMPLETE); | ||
JSONObject array = (JSONObject) p.parse(s); | ||
assertEquals(result, array.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void testObjectCut() throws Exception { | ||
String s = "{\"obj\":"; | ||
String result = "{\"obj\":null}"; | ||
|
||
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE | JSONParser.ACCEPT_INCOMPLETE); | ||
JSONObject array = (JSONObject) p.parse(s); | ||
assertEquals(result, array.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void testObjectCut2() throws Exception { | ||
String s = "{\"obj\""; | ||
String result = "{\"obj\":null}"; | ||
|
||
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE | JSONParser.ACCEPT_INCOMPLETE); | ||
JSONObject array = (JSONObject) p.parse(s); | ||
assertEquals(result, array.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void testObjectCut3() throws Exception { | ||
String s = "{\"obj"; | ||
String result = "{\"obj\":null}"; | ||
|
||
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE | JSONParser.ACCEPT_INCOMPLETE); | ||
JSONObject array = (JSONObject) p.parse(s); | ||
assertEquals(result, array.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void testObjectCut4() throws Exception { | ||
String s = "{\"obj\":\""; | ||
String result = "{\"obj\":\"\"}"; | ||
|
||
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE | JSONParser.ACCEPT_INCOMPLETE); | ||
JSONObject array = (JSONObject) p.parse(s); | ||
assertEquals(result, array.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void testStringCut() throws Exception { | ||
String s = "\"obj"; | ||
String result = "obj"; | ||
|
||
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE | JSONParser.ACCEPT_INCOMPLETE); | ||
String array = (String) p.parse(s); | ||
assertEquals(result, array); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.