Allow different types in JSON array

This commit is contained in:
Fabrice Reix 2022-03-09 09:51:17 +01:00 committed by jcamiel
parent 4917d70af5
commit ce66121d03
6 changed files with 13 additions and 58 deletions

View File

@ -1,4 +1,4 @@
curl 'http://localhost:8000/post-json' -H 'Content-Type: application/json' --data $'{\n "name": "Bob",\n "password": "&secret<>",\n "age": 30,\n "strict": true,\n "spacing": "\\n",\n "g_clef": "\\uD834\\uDD1E"\n}' curl 'http://localhost:8000/post-json' -H 'Content-Type: application/json' --data $'{\n "name": "Bob",\n "password": "&secret<>",\n "age": 30,\n "strict": true,\n "spacing": "\\n",\n "g_clef": "\\uD834\\uDD1E",\n "items": [true, "true", 1]\n}'
curl 'http://localhost:8000/post-json-array' -H 'Content-Type: application/json' --data '[1,2,3]' curl 'http://localhost:8000/post-json-array' -H 'Content-Type: application/json' --data '[1,2,3]'
curl 'http://localhost:8000/post-json-string' -H 'Content-Type: application/json' --data '"Hello"' curl 'http://localhost:8000/post-json-string' -H 'Content-Type: application/json' --data '"Hello"'
curl 'http://localhost:8000/post-json-number' -H 'Content-Type: application/json' --data '100' curl 'http://localhost:8000/post-json-number' -H 'Content-Type: application/json' --data '100'

View File

@ -6,7 +6,8 @@
<span class="line"> "age": 30,</span> <span class="line"> "age": 30,</span>
<span class="line"> "strict": true,</span> <span class="line"> "strict": true,</span>
<span class="line"> "spacing": "\n",</span> <span class="line"> "spacing": "\n",</span>
<span class="line"> "g_clef": "\uD834\uDD1E"</span> <span class="line"> "g_clef": "\uD834\uDD1E",</span>
<span class="line"> "items": [true, "true", 1]</span>
<span class="line">}</span></span> <span class="line">}</span></span>
</span><span class="response"><span class="line"></span> </span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span> <span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>

View File

@ -6,7 +6,8 @@ POST http://localhost:8000/post-json
"age": 30, "age": 30,
"strict": true, "strict": true,
"spacing": "\n", "spacing": "\n",
"g_clef": "\uD834\uDD1E" "g_clef": "\uD834\uDD1E",
"items": [true, "true", 1]
} }
HTTP/1.0 200 HTTP/1.0 200

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-json","body":{"type":"json","value":{"name":"Bob","password":"&secret<>","age":30,"strict":true,"spacing":"\n","g_clef":"ƻǿ"}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-array","body":{"type":"json","value":[1,2,3]}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-string","body":{"type":"json","value":"Hello"}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-number","body":{"type":"json","value":100}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-numbers","body":{"type":"json","value":{"natural":100,"negative":-1,"float":"3.333333333333333","exponent":100e100}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-boolean","body":{"type":"json","value":true}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/get-name"},"response":{"version":"HTTP/1.0","status":200,"captures":[{"name":"name","query":{"type":"body"}}]}},{"request":{"method":"POST","url":"http://localhost:8000/check_name","body":{"type":"json","value":{"name":"{{name}}"}}}}]} {"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-json","body":{"type":"json","value":{"name":"Bob","password":"&secret<>","age":30,"strict":true,"spacing":"\n","g_clef":"ƻǿ","items":[true,"true",1]}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-array","body":{"type":"json","value":[1,2,3]}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-string","body":{"type":"json","value":"Hello"}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-number","body":{"type":"json","value":100}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-numbers","body":{"type":"json","value":{"natural":100,"negative":-1,"float":"3.333333333333333","exponent":100e100}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-boolean","body":{"type":"json","value":true}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/get-name"},"response":{"version":"HTTP/1.0","status":200,"captures":[{"name":"name","query":{"type":"body"}}]}},{"request":{"method":"POST","url":"http://localhost:8000/check_name","body":{"type":"json","value":{"name":"{{name}}"}}}}]}

View File

@ -15,7 +15,8 @@ def post_json():
"age": 30, "age": 30,
"strict": true, "strict": true,
"spacing": "\\n", "spacing": "\\n",
"g_clef": "\\uD834\\uDD1E" "g_clef": "\\uD834\\uDD1E",
"items": [true, \"true\", 1]
}""" }"""
) )
return "" return ""

View File

@ -245,8 +245,8 @@ fn list_value(reader: &mut Reader) -> ParseResult<'static, JsonValue> {
// at least one element // at least one element
if !reader.remaining().starts_with(']') { if !reader.remaining().starts_with(']') {
let first_element = list_element(None, reader)?; let first_element = list_element(reader)?;
elements.push(first_element.clone()); elements.push(first_element);
loop { loop {
if reader.remaining().starts_with(']') { if reader.remaining().starts_with(']') {
@ -256,7 +256,7 @@ fn list_value(reader: &mut Reader) -> ParseResult<'static, JsonValue> {
break; break;
} }
literal(",", reader)?; literal(",", reader)?;
let element = list_element(Some(first_element.value._type()), reader)?; let element = list_element(reader)?;
elements.push(element); elements.push(element);
} }
} }
@ -265,13 +265,9 @@ fn list_value(reader: &mut Reader) -> ParseResult<'static, JsonValue> {
Ok(JsonValue::List { space0, elements }) Ok(JsonValue::List { space0, elements })
} }
fn list_element( fn list_element(reader: &mut Reader) -> ParseResult<'static, JsonListElement> {
_type: Option<String>,
reader: &mut Reader,
) -> ParseResult<'static, JsonListElement> {
let save = reader.state.pos.clone(); let save = reader.state.pos.clone();
let space0 = whitespace(reader); let space0 = whitespace(reader);
let pos = reader.state.pos.clone();
let value = match parse(reader) { let value = match parse(reader) {
Ok(r) => r, Ok(r) => r,
Err(_) => { Err(_) => {
@ -282,15 +278,6 @@ fn list_element(
}) })
} }
}; };
if let Some(t) = _type {
if t != value._type() {
return Err(error::Error {
pos,
recoverable: false,
inner: error::ParseError::Expecting { value: t },
});
}
}
let space1 = whitespace(reader); let space1 = whitespace(reader);
Ok(JsonListElement { Ok(JsonListElement {
space0, space0,
@ -776,17 +763,6 @@ mod tests {
); );
assert!(error.recoverable); assert!(error.recoverable);
let mut reader = Reader::init("[1, true]");
let error = list_value(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 5 });
assert_eq!(
error.inner,
error::ParseError::Expecting {
value: "number".to_string()
}
);
assert!(!error.recoverable);
let mut reader = Reader::init("[1, 2,]"); let mut reader = Reader::init("[1, 2,]");
let error = list_value(&mut reader).err().unwrap(); let error = list_value(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 7 }); assert_eq!(error.pos, Pos { line: 1, column: 7 });
@ -798,7 +774,7 @@ mod tests {
fn test_list_element() { fn test_list_element() {
let mut reader = Reader::init("true"); let mut reader = Reader::init("true");
assert_eq!( assert_eq!(
list_element(None, &mut reader).unwrap(), list_element(&mut reader).unwrap(),
JsonListElement { JsonListElement {
space0: "".to_string(), space0: "".to_string(),
value: JsonValue::Boolean(true), value: JsonValue::Boolean(true),
@ -808,30 +784,6 @@ mod tests {
assert_eq!(reader.state.cursor, 4); assert_eq!(reader.state.cursor, 4);
} }
#[test]
fn test_list_element_error() {
let mut reader = Reader::init("true");
let error = list_element(Some("number".to_string()), &mut reader)
.err()
.unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
error::ParseError::Expecting {
value: "number".to_string()
}
);
assert!(!error.recoverable);
let mut reader = Reader::init("\n]");
let error = list_element(Some("number".to_string()), &mut reader)
.err()
.unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(error.inner, error::ParseError::Json {});
assert!(!error.recoverable);
}
#[test] #[test]
fn test_object_value() { fn test_object_value() {
let mut reader = Reader::init("{}"); let mut reader = Reader::init("{}");