1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use anyhow::Result;
use crate::jsonish::{
parser::{
fixing_parser,
markdown_parser::{self, MarkdownResult},
multi_json_parser,
},
value::Fixes,
Value,
};
use super::ParseOptions;
pub fn parse(str: &str, mut options: ParseOptions) -> Result<Value> {
log::debug!("Parsing:\n{:?}\n-------\n{}\n-------", options, str);
options.depth += 1;
if options.depth > 100 {
return Err(anyhow::anyhow!(
"Depth limit reached. Likely a circular reference."
));
}
match serde_json::from_str(str) {
Ok(v) => return Ok(Value::AnyOf(vec![v], str.to_string())),
Err(e) => {
log::debug!("Invalid JSON: {:?}", e);
}
};
if options.allow_markdown_json {
match markdown_parser::parse(str, &options) {
Ok(items) => match items.len() {
0 => {}
1 => {
let res = items.into_iter().next();
match res {
Some(MarkdownResult::CodeBlock(s, v)) => {
return Ok(Value::AnyOf(
vec![Value::Markdown(s.to_string(), Box::new(v))],
str.to_string(),
));
}
_ => {
log::debug!("Unexpected markdown result: {:?}", res);
}
}
}
_ => {
// In the case of multiple JSON objects:
// Consider it as:
// [item1, item2, ..., itemN, [item1, item2, ..., itemN], str]
// AKA:
// - All the items individually
// - All the items as a list
// - The original string
let others = items
.iter()
.filter_map(|res| match res {
MarkdownResult::String(s) => Some(Value::String(s.to_string())),
_ => None,
})
.map(|v| {
parse(
str,
options.next_from_mode(
crate::jsonish::parser::ParsingMode::JsonMarkdownString,
),
)
})
.filter_map(|res| match res {
Ok(v) => Some(v),
Err(e) => {
log::debug!("Error parsing markdown string: {:?}", e);
None
}
})
.collect::<Vec<_>>();
let items = items
.into_iter()
.filter_map(|res| match res {
MarkdownResult::CodeBlock(s, v) => Some((s, v)),
_ => None,
})
.map(|(s, v)| Value::Markdown(s.to_string(), Box::new(v)))
.collect::<Vec<_>>();
let array = Value::Array(items.clone());
let items = items
.into_iter()
.chain(std::iter::once(array))
.chain(others)
.collect::<Vec<_>>();
return Ok(Value::AnyOf(items, str.to_string()));
}
},
Err(e) => {
log::debug!("Markdown parsing error: {:?}", e);
}
}
}
if options.all_finding_all_json_objects {
match multi_json_parser::parse(str, &options) {
Ok(items) => match items.len() {
0 => {}
1 => {
return Ok(Value::AnyOf(
vec![Value::FixedJson(
items
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("Expected 1 item"))?
.into(),
vec![Fixes::GreppedForJSON],
)],
str.to_string(),
))
}
_ => {
let items_clone = Value::Array(items.clone());
let items = items
.into_iter()
.chain(std::iter::once(items_clone))
.map(|v| Value::FixedJson(v.into(), vec![Fixes::GreppedForJSON]))
.collect::<Vec<_>>();
return Ok(Value::AnyOf(items, str.to_string()));
}
},
Err(e) => {
log::debug!("Error parsing multiple JSON objects: {:?}", e);
}
}
}
if options.allow_fixes {
match fixing_parser::parse(str, &options) {
Ok(items) => {
match items.len() {
0 => {}
1 => {
let (v, fixes) = items.into_iter().next().ok_or_else(|| {
anyhow::anyhow!("Expected 1 item when performing fixes")
})?;
return Ok(Value::AnyOf(
vec![Value::FixedJson(v.into(), fixes)],
str.to_string(),
));
}
_ => {
// In the case of multiple JSON objects:
// Consider it as:
// [item1, item2, ..., itemN, [item1, item2, ..., itemN], str]
// AKA:
// - All the items individually
// - All the items as a list
// - The original string
let items = items
.into_iter()
.map(|(v, fixes)| Value::FixedJson(v.into(), fixes))
.collect::<Vec<_>>();
let items_clone = Value::Array(items.clone());
let items = items
.into_iter()
.chain(std::iter::once(items_clone))
.collect::<Vec<_>>();
return Ok(Value::AnyOf(items, str.to_string()));
}
}
}
Err(e) => {
log::debug!("Error fixing json: {:?}", e);
}
}
}
if options.allow_as_string {
return Ok(Value::String(str.to_string()));
}
Err(anyhow::anyhow!("Failed to parse JSON"))
}