pub mod decoders;
pub mod event_builder;
pub mod expand;
pub mod parser;
pub mod scanner;
pub mod validation;
#[cfg(feature = "async-stream")]
mod async_decode;
use crate::decode::decoders as decoder_impl;
use crate::decode::event_builder::{build_node_from_events, node_to_json};
use crate::decode::expand::expand_paths_safe;
use crate::error::Result;
use crate::options::{DecodeOptions, DecodeStreamOptions, ExpandPathsMode, resolve_decode_options};
use crate::{JsonStreamEvent, JsonValue};
#[cfg(feature = "async-stream")]
pub use async_decode::{
AsyncDecodeStream, decode_stream_async, try_decode_async, try_decode_stream_async,
};
pub fn try_decode(input: &str, options: Option<DecodeOptions>) -> Result<JsonValue> {
let lines = input
.split('\n')
.map(std::string::ToString::to_string)
.collect::<Vec<_>>();
try_decode_from_lines(lines, options)
}
#[must_use]
pub fn decode(input: &str, options: Option<DecodeOptions>) -> JsonValue {
try_decode(input, options).unwrap_or_else(|err| panic!("{err}"))
}
pub fn try_decode_from_lines(
lines: impl IntoIterator<Item = String>,
options: Option<DecodeOptions>,
) -> Result<JsonValue> {
let resolved = resolve_decode_options(options);
let events = decoder_impl::decode_stream_sync(
lines,
Some(DecodeStreamOptions {
indent: Some(resolved.indent),
strict: Some(resolved.strict),
}),
)?;
let mut node = build_node_from_events(events)?;
if resolved.expand_paths == ExpandPathsMode::Safe {
node = expand_paths_safe(node, resolved.strict)?;
}
Ok(node_to_json(node))
}
#[must_use]
pub fn decode_from_lines(
lines: impl IntoIterator<Item = String>,
options: Option<DecodeOptions>,
) -> JsonValue {
try_decode_from_lines(lines, options).unwrap_or_else(|err| panic!("{err}"))
}
pub fn try_decode_stream_sync(
lines: impl IntoIterator<Item = String>,
options: Option<DecodeStreamOptions>,
) -> Result<Vec<JsonStreamEvent>> {
decoder_impl::decode_stream_sync(lines, options)
}
#[must_use]
pub fn decode_stream_sync(
lines: impl IntoIterator<Item = String>,
options: Option<DecodeStreamOptions>,
) -> Vec<JsonStreamEvent> {
try_decode_stream_sync(lines, options).unwrap_or_else(|err| panic!("{err}"))
}
#[cfg(not(feature = "async-stream"))]
pub async fn try_decode_stream(
lines: impl IntoIterator<Item = String>,
options: Option<DecodeStreamOptions>,
) -> Result<Vec<JsonStreamEvent>> {
decoder_impl::decode_stream_sync(lines, options)
}
#[cfg(feature = "async-stream")]
pub async fn try_decode_stream(
lines: impl IntoIterator<Item = String>,
options: Option<DecodeStreamOptions>,
) -> Result<Vec<JsonStreamEvent>> {
async_decode::try_decode_stream_async(lines, options).await
}
#[must_use]
pub async fn decode_stream(
lines: impl IntoIterator<Item = String>,
options: Option<DecodeStreamOptions>,
) -> Vec<JsonStreamEvent> {
try_decode_stream(lines, options)
.await
.unwrap_or_else(|err| panic!("{err}"))
}