Skip to content

Commit c194bdc

Browse files
committed
Better handling of closing brackets in top array / object.
Fixes #12
1 parent efde993 commit c194bdc

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

PhpSerializerNET.Test/DeserializeObjects.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,11 @@ public void DeserializeNestedObject() {
126126
result.Bar.Foo
127127
);
128128
}
129+
130+
[TestMethod]
131+
public void Test_Issue12(){
132+
var result = PhpSerialization.Deserialize("a:1:{i:0;a:4:{s:1:\"A\";s:2:\"63\";s:1:\"B\";a:2:{i:558710;s:1:\"2\";i:558709;s:1:\"2\";}s:1:\"C\";s:2:\"71\";s:1:\"G\";a:3:{s:1:\"x\";s:6:\"446368\";s:1:\"y\";s:1:\"0\";s:1:\"z\";s:5:\"1.029\";}}}");
133+
Assert.IsNotNull(result);
134+
}
129135
}
130136
}

PhpSerializerNET/PhpTokenizer.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public PhpTokenizer(string input, PhpDeserializationOptions options) {
2626
}
2727

2828
internal bool ValidateFormat(ref int position, bool inArray = false) {
29+
bool isTop = position == 0;
30+
bool expectBracket = false;
2931
for (; position < _input.Length; position++) {
3032
switch (_input[position]) {
3133
case 'N': {
@@ -75,8 +77,11 @@ internal bool ValidateFormat(ref int position, bool inArray = false) {
7577
throw new DeserializationException($"Malformed array at position {position}");
7678
}
7779
position += match.Length;
80+
if (isTop) {
81+
expectBracket = true;
82+
}
7883
ValidateFormat(ref position, true);
79-
position++; // Account for the closing bracket.
84+
8085
break;
8186
}
8287
case 'O': {
@@ -85,17 +90,24 @@ internal bool ValidateFormat(ref int position, bool inArray = false) {
8590
throw new DeserializationException($"Malformed object at position {position}");
8691
}
8792
position += match.Length;
93+
if (isTop) {
94+
expectBracket = true;
95+
}
8896
ValidateFormat(ref position, true);
89-
position++; // Account for the closing bracket.
9097

9198
break;
9299
}
93100
case '}': {
94-
if (inArray) {
95-
return true;
101+
if (expectBracket) {
102+
expectBracket = false;
96103
} else {
97-
throw new DeserializationException($"Unexpected token '{_input[position]}' at position {position}.");
104+
if (inArray) {
105+
return true;
106+
} else {
107+
throw new DeserializationException($"Unexpected token '{_input[position]}' at position {position}.");
108+
}
98109
}
110+
break;
99111
}
100112
default: {
101113
throw new DeserializationException($"Unexpected token '{_input[position]}' at position {position}.");

0 commit comments

Comments
 (0)