1use std::collections::HashSet;
2use std::io::BufRead;
3use std::path::Path;
4
5use dataprof_core::{
6 ColumnProfile, DataProfilerError, DataSource, ExecutionMetadata, FileFormat, QualityDimension,
7 SemanticHints,
8};
9use dataprof_runtime::{
10 ProfileReport, ReportAssembler, StreamingColumnCollection, profile_builder,
11};
12use serde::Deserialize;
13use serde_json::Value;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum JsonFormat {
18 JsonArray,
20 Jsonl,
22}
23
24#[derive(Debug, Clone, Default)]
26pub struct JsonParserConfig {
27 pub format: Option<JsonFormat>,
29 pub max_rows: Option<usize>,
31}
32
33impl JsonParserConfig {
34 pub fn with_max_rows(mut self, max_rows: usize) -> Self {
36 self.max_rows = Some(max_rows);
37 self
38 }
39
40 pub fn jsonl() -> Self {
42 Self {
43 format: Some(JsonFormat::Jsonl),
44 ..Default::default()
45 }
46 }
47
48 pub fn json_array() -> Self {
50 Self {
51 format: Some(JsonFormat::JsonArray),
52 ..Default::default()
53 }
54 }
55}
56
57pub type JsonObject = serde_json::Map<String, Value>;
59
60#[derive(Debug, Clone, PartialEq, Eq)]
62pub struct JsonScanSummary {
63 pub rows_read: usize,
64 pub malformed_lines: usize,
65 pub format: FileFormat,
66}
67
68pub fn scan_json_from_reader<R, F>(
73 mut reader: R,
74 config: &JsonParserConfig,
75 mut on_object: F,
76) -> Result<JsonScanSummary, DataProfilerError>
77where
78 R: BufRead,
79 F: FnMut(&JsonObject),
80{
81 let format = match config.format {
82 Some(JsonFormat::JsonArray) => FileFormat::Json,
83 Some(JsonFormat::Jsonl) => FileFormat::Jsonl,
84 None => match consume_leading_whitespace(&mut reader)? {
85 Some(b'[') => FileFormat::Json,
86 _ => FileFormat::Jsonl,
87 },
88 };
89
90 let mut rows_read = 0;
91 let mut malformed_lines = 0;
92
93 match format {
94 FileFormat::Jsonl => loop {
95 if let Some(max) = config.max_rows
96 && rows_read >= max
97 {
98 break;
99 }
100
101 let mut deserializer = serde_json::Deserializer::from_reader(&mut reader);
102 let value: Value = match Value::deserialize(&mut deserializer) {
103 Ok(v) => v,
104 Err(err) if err.classify() == serde_json::error::Category::Eof => break,
105 Err(_) => {
106 malformed_lines += 1;
107 skip_to_next_line(&mut reader)?;
108 continue;
109 }
110 };
111
112 if let Value::Object(ref obj) = value {
113 on_object(obj);
114 rows_read += 1;
115 }
116 },
117 FileFormat::Json => {
118 let mut found_array = false;
119 loop {
120 let mut consume = 0;
121 {
122 let buf = reader.fill_buf().map_err(DataProfilerError::from)?;
123 if buf.is_empty() {
124 break;
125 }
126 for &byte in buf {
127 consume += 1;
128 if byte == b'[' {
129 found_array = true;
130 break;
131 } else if !byte.is_ascii_whitespace() {
132 break;
133 }
134 }
135 }
136 reader.consume(consume);
137 if found_array || consume == 0 {
138 break;
139 }
140 }
141
142 if !found_array {
143 if config.format.is_some() {
144 return Err(DataProfilerError::JsonParsingError {
145 message: "Expected JSON array (starts with '[') but input does not match"
146 .to_string(),
147 });
148 }
149 } else {
150 loop {
151 let mut consume = 0;
152 let mut found_value = false;
153 let mut end_of_array = false;
154
155 {
156 let buf = reader.fill_buf().map_err(DataProfilerError::from)?;
157 if buf.is_empty() {
158 break;
159 }
160 for &byte in buf {
161 if byte.is_ascii_whitespace() || byte == b',' {
162 consume += 1;
163 } else if byte == b']' {
164 end_of_array = true;
165 consume += 1;
166 break;
167 } else {
168 found_value = true;
169 break;
170 }
171 }
172 }
173
174 reader.consume(consume);
175 if end_of_array {
176 break;
177 }
178
179 if found_value {
180 if let Some(max) = config.max_rows
181 && rows_read >= max
182 {
183 break;
184 }
185
186 let mut deserializer = serde_json::Deserializer::from_reader(&mut reader);
187 match Value::deserialize(&mut deserializer) {
188 Ok(Value::Object(obj)) => {
189 on_object(&obj);
190 rows_read += 1;
191 }
192 Ok(_) => {}
193 Err(_) => {
194 malformed_lines += 1;
195 break;
196 }
197 }
198 }
199 }
200 }
201 }
202 _ => unreachable!("json scanner only returns json or jsonl formats"),
203 }
204
205 Ok(JsonScanSummary {
206 rows_read,
207 malformed_lines,
208 format,
209 })
210}
211
212fn consume_leading_whitespace<R: BufRead>(reader: &mut R) -> Result<Option<u8>, DataProfilerError> {
213 loop {
214 let mut bytes_to_consume = 0;
215 let first_non_whitespace = {
216 let buf = reader.fill_buf().map_err(DataProfilerError::from)?;
217 if buf.is_empty() {
218 return Ok(None);
219 }
220
221 let first_non_whitespace = buf.iter().find(|byte| !byte.is_ascii_whitespace()).copied();
222 if first_non_whitespace.is_none() {
223 bytes_to_consume = buf.len();
224 }
225 first_non_whitespace
226 };
227
228 if first_non_whitespace.is_some() {
229 return Ok(first_non_whitespace);
230 }
231
232 reader.consume(bytes_to_consume);
233 }
234}
235
236fn skip_to_next_line<R: BufRead>(reader: &mut R) -> Result<(), DataProfilerError> {
237 let mut discarded = Vec::new();
238 reader
239 .read_until(b'\n', &mut discarded)
240 .map_err(DataProfilerError::from)?;
241 Ok(())
242}
243
244fn json_value_to_string(value: &Value) -> String {
246 match value {
247 Value::Null => String::new(),
248 Value::Bool(boolean) => boolean.to_string(),
249 Value::Number(number) => number.to_string(),
250 Value::String(string) => string.to_string(),
251 Value::Array(_) | Value::Object(_) => serde_json::to_string(value).unwrap_or_default(),
252 }
253}
254
255fn feed_json_object(
257 obj: &JsonObject,
258 known_columns: &mut Vec<String>,
259 known_columns_set: &mut HashSet<String>,
260 column_stats: &mut StreamingColumnCollection,
261) {
262 for key in obj.keys() {
263 if known_columns_set.insert(key.clone()) {
264 known_columns.push(key.clone());
265 }
266 }
267
268 let values: Vec<String> = known_columns
269 .iter()
270 .map(|column| {
271 obj.get(column)
272 .map(json_value_to_string)
273 .unwrap_or_default()
274 })
275 .collect();
276
277 column_stats.process_record(known_columns, values);
278}
279
280pub fn analyze_json_from_reader<R: BufRead>(
284 reader: R,
285 config: &JsonParserConfig,
286) -> Result<
287 (
288 Vec<ColumnProfile>,
289 StreamingColumnCollection,
290 usize,
291 usize,
292 FileFormat,
293 ),
294 DataProfilerError,
295> {
296 analyze_json_from_reader_with_hints(reader, config, &SemanticHints::default())
297}
298
299pub fn analyze_json_from_reader_with_hints<R: BufRead>(
300 reader: R,
301 config: &JsonParserConfig,
302 semantic_hints: &SemanticHints,
303) -> Result<
304 (
305 Vec<ColumnProfile>,
306 StreamingColumnCollection,
307 usize,
308 usize,
309 FileFormat,
310 ),
311 DataProfilerError,
312> {
313 let mut column_stats = StreamingColumnCollection::new();
314 let mut known_columns = Vec::new();
315 let mut known_columns_set = HashSet::new();
316
317 let summary = scan_json_from_reader(reader, config, |obj| {
318 feed_json_object(
319 obj,
320 &mut known_columns,
321 &mut known_columns_set,
322 &mut column_stats,
323 );
324 })?;
325
326 let profiles = profile_builder::profiles_from_streaming_with_hints(
327 &column_stats,
328 false,
329 false,
330 None,
331 semantic_hints,
332 );
333
334 Ok((
335 profiles,
336 column_stats,
337 summary.rows_read,
338 summary.malformed_lines,
339 summary.format,
340 ))
341}
342
343pub fn analyze_json_file(
345 file_path: &Path,
346 config: &JsonParserConfig,
347) -> Result<ProfileReport, DataProfilerError> {
348 analyze_json_file_with_dimensions(file_path, config, None)
349}
350
351pub fn analyze_json_file_with_dimensions(
353 file_path: &Path,
354 config: &JsonParserConfig,
355 quality_dimensions: Option<&[QualityDimension]>,
356) -> Result<ProfileReport, DataProfilerError> {
357 analyze_json_file_with_dimensions_and_hints(
358 file_path,
359 config,
360 quality_dimensions,
361 &SemanticHints::default(),
362 )
363}
364
365pub fn analyze_json_file_with_dimensions_and_hints(
366 file_path: &Path,
367 config: &JsonParserConfig,
368 quality_dimensions: Option<&[QualityDimension]>,
369 semantic_hints: &SemanticHints,
370) -> Result<ProfileReport, DataProfilerError> {
371 let metadata = std::fs::metadata(file_path).map_err(|error| map_io_error(file_path, error))?;
372 let start = std::time::Instant::now();
373
374 let file = std::fs::File::open(file_path).map_err(|error| map_io_error(file_path, error))?;
375 let buf_reader = std::io::BufReader::new(file);
376
377 let (column_profiles, column_stats, rows_read, malformed_lines, format) =
378 analyze_json_from_reader_with_hints(buf_reader, config, semantic_hints)?;
379
380 let file_source = DataSource::File {
381 path: file_path.display().to_string(),
382 format,
383 size_bytes: metadata.len(),
384 modified_at: None,
385 parquet_metadata: None,
386 };
387
388 if rows_read == 0 {
389 if malformed_lines > 0 {
390 return Err(DataProfilerError::JsonParsingError {
391 message: "No valid JSON records found in file (malformed JSON encountered)"
392 .to_string(),
393 });
394 }
395 return Ok(ReportAssembler::new(
396 file_source,
397 ExecutionMetadata::new(0, 0, start.elapsed().as_millis()),
398 )
399 .skip_quality()
400 .build());
401 }
402
403 let sample_columns = profile_builder::quality_check_samples(&column_stats);
404 let scan_time_ms = start.elapsed().as_millis();
405 let num_columns = column_profiles.len();
406
407 let mut assembler = ReportAssembler::new(
408 file_source,
409 ExecutionMetadata::new(rows_read, num_columns, scan_time_ms),
410 )
411 .columns(column_profiles)
412 .with_quality_data(sample_columns)
413 .with_semantic_hints(semantic_hints.clone());
414 if let Some(dimensions) = quality_dimensions {
415 assembler = assembler.with_requested_dimensions(dimensions.to_vec());
416 }
417 Ok(assembler.build())
418}
419
420fn map_io_error(file_path: &Path, error: std::io::Error) -> DataProfilerError {
421 if error.kind() == std::io::ErrorKind::NotFound {
422 DataProfilerError::FileNotFound {
423 path: file_path.display().to_string(),
424 }
425 } else {
426 DataProfilerError::from(error)
427 }
428}
429
430#[cfg(test)]
431mod tests {
432 use super::*;
433 use std::io::{Cursor, Write};
434 use tempfile::NamedTempFile;
435
436 fn write_file(content: &str) -> NamedTempFile {
437 let mut file = NamedTempFile::new().unwrap();
438 write!(file, "{}", content).unwrap();
439 file.flush().unwrap();
440 file
441 }
442
443 #[test]
444 fn test_scan_jsonl_detects_format_and_rows() {
445 let data = b"{\"x\":1}\n{\"x\":2}\n{\"x\":3}\n";
446 let mut rows = 0;
447
448 let summary = scan_json_from_reader(
449 Cursor::new(data.as_ref()),
450 &JsonParserConfig::default(),
451 |_| {
452 rows += 1;
453 },
454 )
455 .unwrap();
456
457 assert_eq!(summary.format, FileFormat::Jsonl);
458 assert_eq!(summary.rows_read, 3);
459 assert_eq!(summary.malformed_lines, 0);
460 assert_eq!(rows, 3);
461 }
462
463 #[test]
464 fn test_scan_json_array_detects_format_and_rows() {
465 let data = br#"[{"name":"Alice"},{"name":"Bob"}]"#;
466 let mut keys = Vec::new();
467
468 let summary = scan_json_from_reader(
469 Cursor::new(data.as_ref()),
470 &JsonParserConfig::default(),
471 |obj| keys.push(obj.keys().cloned().collect::<Vec<_>>()),
472 )
473 .unwrap();
474
475 assert_eq!(summary.format, FileFormat::Json);
476 assert_eq!(summary.rows_read, 2);
477 assert_eq!(keys.len(), 2);
478 assert_eq!(keys[0], vec!["name".to_string()]);
479 }
480
481 #[test]
482 fn test_scan_respects_max_rows() {
483 let data = b"{\"x\":1}\n{\"x\":2}\n{\"x\":3}\n{\"x\":4}\n";
484 let config = JsonParserConfig::default().with_max_rows(2);
485 let mut rows = 0;
486
487 let summary =
488 scan_json_from_reader(Cursor::new(data.as_ref()), &config, |_| rows += 1).unwrap();
489
490 assert_eq!(summary.rows_read, 2);
491 assert_eq!(rows, 2);
492 }
493
494 #[test]
495 fn test_scan_skips_malformed_jsonl_lines() {
496 let data = b"{\"x\":1}\n{\"x\":,bad}\n{\"x\":3}\n";
497 let mut rows = 0;
498
499 let summary = scan_json_from_reader(
500 Cursor::new(data.as_ref()),
501 &JsonParserConfig::jsonl(),
502 |_| {
503 rows += 1;
504 },
505 )
506 .unwrap();
507
508 assert_eq!(summary.format, FileFormat::Jsonl);
509 assert_eq!(summary.rows_read, 2);
510 assert_eq!(summary.malformed_lines, 1);
511 assert_eq!(rows, 2);
512 }
513
514 #[test]
515 fn test_scan_pretty_printed_root_object_is_one_jsonl_row() {
516 let data = br#"{
517 "type": "FeatureCollection",
518 "features": [
519 {"id": 1},
520 {"id": 2}
521 ]
522}"#;
523 let mut object_field_counts = Vec::new();
524
525 let summary = scan_json_from_reader(
526 Cursor::new(data.as_ref()),
527 &JsonParserConfig::default(),
528 |obj| object_field_counts.push(obj.len()),
529 )
530 .unwrap();
531
532 assert_eq!(summary.format, FileFormat::Jsonl);
533 assert_eq!(summary.rows_read, 1);
534 assert_eq!(summary.malformed_lines, 0);
535 assert_eq!(object_field_counts, vec![2]);
536 }
537
538 #[test]
539 fn test_forced_json_array_requires_opening_bracket() {
540 let data = br#"{"x":1}"#;
541 let err = scan_json_from_reader(
542 Cursor::new(data.as_ref()),
543 &JsonParserConfig::json_array(),
544 |_| {},
545 )
546 .expect_err("forced json array should reject non-array input");
547
548 assert!(err.to_string().contains("Expected JSON array"));
549 }
550
551 #[test]
552 fn test_analyze_json_from_reader_jsonl_streaming() {
553 let data = b"{\"x\":1,\"y\":\"a\"}\n{\"x\":2,\"y\":\"b\"}\n{\"x\":3,\"y\":\"c\"}\n";
554 let cursor = Cursor::new(data.as_ref());
555 let config = JsonParserConfig::default();
556
557 let (profiles, _stats, rows, _malformed, format) =
558 analyze_json_from_reader(cursor, &config).unwrap();
559 assert_eq!(format, FileFormat::Jsonl);
560 assert_eq!(rows, 3);
561 assert_eq!(profiles.len(), 2);
562 }
563
564 #[test]
565 fn test_analyze_json_from_reader_json_array() {
566 let data = br#"[{"name":"Alice","age":25},{"name":"Bob","age":30}]"#;
567 let cursor = Cursor::new(data.as_ref());
568 let config = JsonParserConfig::default();
569
570 let (profiles, _stats, rows, _malformed, format) =
571 analyze_json_from_reader(cursor, &config).unwrap();
572 assert_eq!(format, FileFormat::Json);
573 assert_eq!(rows, 2);
574 assert_eq!(profiles.len(), 2);
575 }
576
577 #[test]
578 fn test_analyze_json_from_reader_max_rows() {
579 let data = b"{\"x\":1}\n{\"x\":2}\n{\"x\":3}\n{\"x\":4}\n{\"x\":5}\n";
580 let cursor = Cursor::new(data.as_ref());
581 let config = JsonParserConfig::default().with_max_rows(3);
582
583 let (_profiles, _stats, rows, _malformed, _format) =
584 analyze_json_from_reader(cursor, &config).unwrap();
585 assert_eq!(rows, 3);
586 }
587
588 #[test]
589 fn test_analyze_json_from_reader_missing_fields() {
590 let data = b"{\"a\":1,\"b\":2}\n{\"a\":3}\n";
591 let cursor = Cursor::new(data.as_ref());
592 let config = JsonParserConfig::jsonl();
593
594 let (profiles, _stats, rows, _malformed, _format) =
595 analyze_json_from_reader(cursor, &config).unwrap();
596 assert_eq!(rows, 2);
597
598 let col_b = profiles.iter().find(|profile| profile.name == "b").unwrap();
599 assert_eq!(col_b.total_count, 2);
600 }
601
602 #[test]
603 fn test_analyze_json_file_quality_report() {
604 let file = write_file(r#"[{"x":1},{"x":2}]"#);
605 let config = JsonParserConfig::default();
606 let report = analyze_json_file(file.path(), &config).unwrap();
607
608 assert_eq!(report.execution.rows_processed, 2);
609 assert_eq!(report.column_profiles.len(), 1);
610 assert!(report.quality_score().unwrap() >= 0.0);
611 }
612
613 #[test]
614 fn test_jsonl_skips_malformed_lines() {
615 let data = b"{\"x\":1}\n{\"x\":,malformed}\n{\"x\":3}\n";
616 let cursor = Cursor::new(data.as_ref());
617 let config = JsonParserConfig::jsonl();
618
619 let (profiles, _stats, rows, _malformed, format) =
620 analyze_json_from_reader(cursor, &config).unwrap();
621 assert_eq!(format, FileFormat::Jsonl);
622 assert_eq!(rows, 2);
623 assert_eq!(profiles[0].total_count, 2);
624 }
625
626 #[test]
627 fn test_analyze_json_with_large_leading_whitespace() {
628 let data = format!("{}[{{\"x\":1}}]", " ".repeat(10_000));
629 let cursor = Cursor::new(data.into_bytes());
630 let config = JsonParserConfig::default();
631
632 let (_profiles, _stats, rows, malformed, format) =
633 analyze_json_from_reader(cursor, &config).unwrap();
634 assert_eq!(format, FileFormat::Json);
635 assert_eq!(rows, 1);
636 assert_eq!(malformed, 0);
637 }
638
639 #[test]
640 fn test_analyze_json_array() {
641 let json = write_file(r#"[{"name":"Alice","age":25},{"name":"Bob","age":30}]"#);
642 let config = JsonParserConfig::default();
643 let report = analyze_json_file(json.path(), &config).unwrap();
644 let profiles = &report.column_profiles;
645
646 assert_eq!(profiles.len(), 2);
647 let names: Vec<&str> = profiles
648 .iter()
649 .map(|profile| profile.name.as_str())
650 .collect();
651 assert!(names.contains(&"name"));
652 assert!(names.contains(&"age"));
653
654 let age = profiles
655 .iter()
656 .find(|profile| profile.name == "age")
657 .unwrap();
658 assert_eq!(age.total_count, 2);
659 assert_eq!(age.null_count, 0);
660 }
661
662 #[test]
663 fn test_analyze_jsonl() {
664 let jsonl = write_file("{\"x\":1}\n{\"x\":2}\n{\"x\":3}\n");
665 let config = JsonParserConfig::default();
666 let report = analyze_json_file(jsonl.path(), &config).unwrap();
667 let profiles = &report.column_profiles;
668
669 assert_eq!(profiles.len(), 1);
670 assert_eq!(profiles[0].name, "x");
671 assert_eq!(profiles[0].total_count, 3);
672 }
673
674 #[test]
675 fn test_analyze_json_with_nulls() {
676 let json = write_file(r#"[{"a":"hello","b":1},{"a":null,"b":2},{"a":"world","b":null}]"#);
677 let config = JsonParserConfig::default();
678 let report = analyze_json_file(json.path(), &config).unwrap();
679 let profiles = &report.column_profiles;
680
681 let col_a = profiles.iter().find(|profile| profile.name == "a").unwrap();
682 assert_eq!(col_a.null_count, 1);
683
684 let col_b = profiles.iter().find(|profile| profile.name == "b").unwrap();
685 assert_eq!(col_b.null_count, 1);
686 }
687
688 #[test]
689 fn test_analyze_json_with_missing_fields() {
690 let json = write_file(r#"[{"a":1,"b":2},{"a":3}]"#);
691 let config = JsonParserConfig::default();
692 let report = analyze_json_file(json.path(), &config).unwrap();
693 let profiles = &report.column_profiles;
694
695 let col_b = profiles.iter().find(|profile| profile.name == "b").unwrap();
696 assert_eq!(col_b.total_count, 2);
697 }
698
699 #[test]
700 fn test_analyze_json_empty_array() {
701 let json = write_file("[]");
702 let config = JsonParserConfig::default();
703 let report = analyze_json_file(json.path(), &config).unwrap();
704 assert!(report.column_profiles.is_empty());
705 }
706
707 #[test]
708 fn test_analyze_json_malformed_returns_error() {
709 let json = write_file("this is entirely invalid json");
710 let config = JsonParserConfig::default();
711 let err = analyze_json_file(json.path(), &config)
712 .expect_err("malformed JSON should return an error");
713
714 let message = err.to_string().to_lowercase();
715 assert!(message.contains("malformed") && message.contains("json"));
716 }
717
718 #[test]
719 fn test_analyze_json_file_detects_format() {
720 let json_array = write_file(r#"[{"x":1}]"#);
721 let config = JsonParserConfig::default();
722 let report = analyze_json_file(json_array.path(), &config).unwrap();
723 assert!(matches!(
724 report.data_source,
725 DataSource::File {
726 format: FileFormat::Json,
727 ..
728 }
729 ));
730
731 let jsonl = write_file("{\"x\":1}\n{\"x\":2}\n");
732 let report = analyze_json_file(jsonl.path(), &config).unwrap();
733 assert!(matches!(
734 report.data_source,
735 DataSource::File {
736 format: FileFormat::Jsonl,
737 ..
738 }
739 ));
740 }
741
742 #[test]
743 fn test_analyze_json_file_empty() {
744 let json = write_file("");
745 let config = JsonParserConfig::default();
746 let report = analyze_json_file(json.path(), &config).unwrap();
747 assert_eq!(report.execution.rows_processed, 0);
748 assert!(report.column_profiles.is_empty());
749 }
750
751 #[test]
752 fn test_analyze_json_boolean_and_nested() {
753 let json =
754 write_file(r#"[{"flag":true,"nested":{"a":1}},{"flag":false,"nested":{"b":2}}]"#);
755 let config = JsonParserConfig::default();
756 let report = analyze_json_file(json.path(), &config).unwrap();
757 let profiles = &report.column_profiles;
758
759 let flag = profiles
760 .iter()
761 .find(|profile| profile.name == "flag")
762 .unwrap();
763 assert_eq!(flag.total_count, 2);
764
765 let nested = profiles
766 .iter()
767 .find(|profile| profile.name == "nested")
768 .unwrap();
769 assert_eq!(nested.total_count, 2);
770 }
771
772 #[test]
773 fn test_single_root_object_compact_yields_one_row() {
774 let data = br#"{"type":"FeatureCollection","features":[1,2,3]}"#;
775 let cursor = Cursor::new(data.as_ref());
776 let config = JsonParserConfig::default();
777
778 let (profiles, _stats, rows, malformed, _format) =
779 analyze_json_from_reader(cursor, &config).unwrap();
780 assert_eq!(rows, 1);
781 assert_eq!(malformed, 0);
782 assert_eq!(profiles.len(), 2);
783
784 let names: Vec<&str> = profiles
785 .iter()
786 .map(|profile| profile.name.as_str())
787 .collect();
788 assert!(names.contains(&"type"));
789 assert!(names.contains(&"features"));
790 }
791
792 #[test]
793 fn test_jsonl_multi_object_still_works() {
794 let data = b"{\"x\":1}\n{\"x\":2}\n{\"x\":3}\n";
795 let cursor = Cursor::new(data.as_ref());
796 let config = JsonParserConfig::default();
797
798 let (_profiles, _stats, rows, _malformed, format) =
799 analyze_json_from_reader(cursor, &config).unwrap();
800 assert_eq!(format, FileFormat::Jsonl);
801 assert_eq!(rows, 3);
802 }
803
804 #[test]
805 fn test_single_root_object_via_analyze_json_file() {
806 let file = write_file(r#"{"type":"FeatureCollection","features":[{"id":1},{"id":2}]}"#);
807 let config = JsonParserConfig::default();
808 let report = analyze_json_file(file.path(), &config).unwrap();
809
810 assert_eq!(report.execution.rows_processed, 1);
811 assert_eq!(report.column_profiles.len(), 2);
812 }
813
814 #[test]
815 fn test_single_root_object_pretty_printed_yields_one_row() {
816 let data = br#"{
817 "type": "FeatureCollection",
818 "features": [
819 {"id": 1},
820 {"id": 2}
821 ]
822}"#;
823 let cursor = Cursor::new(data.as_ref());
824 let config = JsonParserConfig::default();
825
826 let (profiles, _stats, rows, malformed, format) =
827 analyze_json_from_reader(cursor, &config).unwrap();
828 assert_eq!(format, FileFormat::Jsonl);
829 assert_eq!(rows, 1);
830 assert_eq!(malformed, 0);
831 assert_eq!(profiles.len(), 2);
832 }
833
834 #[test]
835 fn test_single_root_object_pretty_printed_via_analyze_json_file() {
836 let file = write_file(
837 "{\n \"type\": \"FeatureCollection\",\n \"features\": [\n {\"id\": 1},\n {\"id\": 2}\n ]\n}\n",
838 );
839 let config = JsonParserConfig::default();
840 let report = analyze_json_file(file.path(), &config).unwrap();
841
842 assert_eq!(report.execution.rows_processed, 1);
843 assert_eq!(report.column_profiles.len(), 2);
844 }
845}