Skip to content

Commit

Permalink
Fix in json parser (#358)
Browse files Browse the repository at this point in the history
- added testFormatting6-8 covering the issue, fix in CLParser
- added iterator for CLObject
  • Loading branch information
camaelon authored Jul 28, 2021
1 parent 85991a3 commit 4880da5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public ArrayList<String> names() {

public boolean has(String name) {
for (CLElement element : mElements) {
CLKey key = (CLKey) element;
if (key.content().equals(name)) {
return true;
if (element instanceof CLKey) {
CLKey key = (CLKey) element;
if (key.content().equals(name)) {
return true;
}
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public static CLElement allocate(String name, CLElement value) {
return key;
}

public String getName() { return content(); }

protected String toJSON() {
if (mElements.size() > 0) {
return getDebugName() + content() + ": " + mElements.get(0).toJSON();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package androidx.constraintlayout.core.parser;

public class CLObject extends CLContainer {
import java.util.Iterator;

public class CLObject extends CLContainer implements Iterable<CLKey>{

public CLObject(char[] content) {
super(content);
Expand Down Expand Up @@ -62,4 +64,28 @@ public String toFormattedJSON(int indent, int forceIndent) {
return json.toString();
}

@Override
public Iterator iterator() {
return new CLObjectIterator(this);
}

private class CLObjectIterator implements Iterator {
CLObject myObject;
int index = 0;
public CLObjectIterator(CLObject clObject) {
myObject = clObject;
}

@Override
public boolean hasNext() {
return index < myObject.size();
}

@Override
public Object next() {
CLKey key = (CLKey) myObject.mElements.get(index);
index++;
return key;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public CLObject parse() throws CLParsingException {
if (c == '}' || c == ']') {
currentElement = currentElement.getContainer();
currentElement.setEnd(i - 1);
if (currentElement instanceof CLKey) {
currentElement = currentElement.getContainer();
currentElement.setEnd(i - 1);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,63 @@ public void testFormatting5() {
e.printStackTrace();
}
}

@Test
public void testFormatting6() {
String test = "{ root: {interpolated: {left: 0, top: 0, right: 800, bottom: 772}}, " +
"button: {interpolated: {left: 0, top: 372, right: 800, bottom: 401}}, " +
"text1: {interpolated: {left: 100, top: 285, right: 208, bottom: 301}}, " +
"text2: {interpolated: {left: 723, top: 736, right: 780, bottom: 752}}, " +
"g1: {type: 'vGuideline',interpolated: {left: 100, top: 0, right: 100, bottom: 772}}, }";
try {
CLObject parsedContent = CLParser.parse(test);
assertEquals("{\n" +
" root: { interpolated: { left: 0, top: 0, right: 800, bottom: 772 } },\n" +
" button: { interpolated: { left: 0, top: 372, right: 800, bottom: 401 } },\n" +
" text1: { interpolated: { left: 100, top: 285, right: 208, bottom: 301 } },\n" +
" text2: { interpolated: { left: 723, top: 736, right: 780, bottom: 752 } },\n" +
" g1: {\n" +
" type: 'vGuideline',\n" +
" interpolated: { left: 100, top: 0, right: 100, bottom: 772 }\n" +
" }\n" +
"}", parsedContent.toFormattedJSON());
} catch (CLParsingException e) {
System.err.println("Exception " + e.reason());
e.printStackTrace();
}
}

@Test
public void testFormatting7() {
String test = "{ root: {left: 0, top: 0, right: 800, bottom: 772}, " +
"button: {left: 0, top: 372, right: 800, bottom: 401}, ";
try {
CLObject parsedContent = CLParser.parse(test);
assertEquals("{\n" +
" root: { left: 0, top: 0, right: 800, bottom: 772 },\n" +
" button: { left: 0, top: 372, right: 800, bottom: 401 }\n" +
"}", parsedContent.toFormattedJSON());
} catch (CLParsingException e) {
System.err.println("Exception " + e.reason());
e.printStackTrace();
}
}

@Test
public void testFormatting8() {
String test = "{ root: { bottom: 772}, " +
"button: { bottom: 401 }, ";
try {
CLObject parsedContent = CLParser.parse(test);
assertEquals("{\n" +
" root: { bottom: 772 },\n" +
" button: { bottom: 401 }\n" +
"}", parsedContent.toFormattedJSON());
} catch (CLParsingException e) {
System.err.println("Exception " + e.reason());
e.printStackTrace();
}
}
}


0 comments on commit 4880da5

Please sign in to comment.