Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 4880da5

Browse files
authored
Fix in json parser (#358)
- added testFormatting6-8 covering the issue, fix in CLParser - added iterator for CLObject
1 parent 85991a3 commit 4880da5

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

constraintlayout/core/src/main/java/androidx/constraintlayout/core/parser/CLContainer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public ArrayList<String> names() {
6464

6565
public boolean has(String name) {
6666
for (CLElement element : mElements) {
67-
CLKey key = (CLKey) element;
68-
if (key.content().equals(name)) {
69-
return true;
67+
if (element instanceof CLKey) {
68+
CLKey key = (CLKey) element;
69+
if (key.content().equals(name)) {
70+
return true;
71+
}
7072
}
7173
}
7274
return false;

constraintlayout/core/src/main/java/androidx/constraintlayout/core/parser/CLKey.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public static CLElement allocate(String name, CLElement value) {
4848
return key;
4949
}
5050

51+
public String getName() { return content(); }
52+
5153
protected String toJSON() {
5254
if (mElements.size() > 0) {
5355
return getDebugName() + content() + ": " + mElements.get(0).toJSON();

constraintlayout/core/src/main/java/androidx/constraintlayout/core/parser/CLObject.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package androidx.constraintlayout.core.parser;
1717

18-
public class CLObject extends CLContainer {
18+
import java.util.Iterator;
19+
20+
public class CLObject extends CLContainer implements Iterable<CLKey>{
1921

2022
public CLObject(char[] content) {
2123
super(content);
@@ -62,4 +64,28 @@ public String toFormattedJSON(int indent, int forceIndent) {
6264
return json.toString();
6365
}
6466

67+
@Override
68+
public Iterator iterator() {
69+
return new CLObjectIterator(this);
70+
}
71+
72+
private class CLObjectIterator implements Iterator {
73+
CLObject myObject;
74+
int index = 0;
75+
public CLObjectIterator(CLObject clObject) {
76+
myObject = clObject;
77+
}
78+
79+
@Override
80+
public boolean hasNext() {
81+
return index < myObject.size();
82+
}
83+
84+
@Override
85+
public Object next() {
86+
CLKey key = (CLKey) myObject.mElements.get(index);
87+
index++;
88+
return key;
89+
}
90+
}
6591
}

constraintlayout/core/src/main/java/androidx/constraintlayout/core/parser/CLParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ public CLObject parse() throws CLParsingException {
124124
if (c == '}' || c == ']') {
125125
currentElement = currentElement.getContainer();
126126
currentElement.setEnd(i - 1);
127+
if (currentElement instanceof CLKey) {
128+
currentElement = currentElement.getContainer();
129+
currentElement.setEnd(i - 1);
130+
}
127131
}
128132
}
129133
}

constraintlayout/core/src/test/java/androidx/constraintlayout/core/parser/CLParserTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,63 @@ public void testFormatting5() {
680680
e.printStackTrace();
681681
}
682682
}
683+
684+
@Test
685+
public void testFormatting6() {
686+
String test = "{ root: {interpolated: {left: 0, top: 0, right: 800, bottom: 772}}, " +
687+
"button: {interpolated: {left: 0, top: 372, right: 800, bottom: 401}}, " +
688+
"text1: {interpolated: {left: 100, top: 285, right: 208, bottom: 301}}, " +
689+
"text2: {interpolated: {left: 723, top: 736, right: 780, bottom: 752}}, " +
690+
"g1: {type: 'vGuideline',interpolated: {left: 100, top: 0, right: 100, bottom: 772}}, }";
691+
try {
692+
CLObject parsedContent = CLParser.parse(test);
693+
assertEquals("{\n" +
694+
" root: { interpolated: { left: 0, top: 0, right: 800, bottom: 772 } },\n" +
695+
" button: { interpolated: { left: 0, top: 372, right: 800, bottom: 401 } },\n" +
696+
" text1: { interpolated: { left: 100, top: 285, right: 208, bottom: 301 } },\n" +
697+
" text2: { interpolated: { left: 723, top: 736, right: 780, bottom: 752 } },\n" +
698+
" g1: {\n" +
699+
" type: 'vGuideline',\n" +
700+
" interpolated: { left: 100, top: 0, right: 100, bottom: 772 }\n" +
701+
" }\n" +
702+
"}", parsedContent.toFormattedJSON());
703+
} catch (CLParsingException e) {
704+
System.err.println("Exception " + e.reason());
705+
e.printStackTrace();
706+
}
707+
}
708+
709+
@Test
710+
public void testFormatting7() {
711+
String test = "{ root: {left: 0, top: 0, right: 800, bottom: 772}, " +
712+
"button: {left: 0, top: 372, right: 800, bottom: 401}, ";
713+
try {
714+
CLObject parsedContent = CLParser.parse(test);
715+
assertEquals("{\n" +
716+
" root: { left: 0, top: 0, right: 800, bottom: 772 },\n" +
717+
" button: { left: 0, top: 372, right: 800, bottom: 401 }\n" +
718+
"}", parsedContent.toFormattedJSON());
719+
} catch (CLParsingException e) {
720+
System.err.println("Exception " + e.reason());
721+
e.printStackTrace();
722+
}
723+
}
724+
725+
@Test
726+
public void testFormatting8() {
727+
String test = "{ root: { bottom: 772}, " +
728+
"button: { bottom: 401 }, ";
729+
try {
730+
CLObject parsedContent = CLParser.parse(test);
731+
assertEquals("{\n" +
732+
" root: { bottom: 772 },\n" +
733+
" button: { bottom: 401 }\n" +
734+
"}", parsedContent.toFormattedJSON());
735+
} catch (CLParsingException e) {
736+
System.err.println("Exception " + e.reason());
737+
e.printStackTrace();
738+
}
739+
}
683740
}
684741

685742

0 commit comments

Comments
 (0)