Struct moth_wasm::JsonFile

source ·
pub struct JsonFile { /* private fields */ }
Expand description

A JSON File, stored in a “self-recursive” Map<u64, Value>

Note: If values are changed at some point, make sure that Self::remove_null is called after the changes have been done: when a nested structure is removed, all contained values are changed to Null; calling Self::remove_null is actually equivalent to a garbage collection process, in this context.

Note: Object keys are stored in a [Pool], which makes this well suited for parsing large arrays of objects with similar layouts.

Implementations§

source§

impl JsonFile

source

pub fn new(json: Option<&str>) -> Result<JsonFile, &'static str>

Creates a new JSON file (optionally from a JSON string)

source

pub fn with_key_pool( json: Option<&str>, keys: Pool ) -> Result<JsonFile, &'static str>

Creates a new JSON file using an existing key string pool (optionally from a JSON string)

source

pub fn get(&self, path: &Path) -> &Value

source

pub fn remove_null(&mut self)

Efficiently removes all Null values from the file.

Note: if a Null value was inserted in an array or an object, the key/index will persist, but the actual value will be removed.

source

pub fn remove(&mut self, path: &Path)

Removes a value and all its eventual content.

source

pub fn set_array(&mut self, path: &Path)

Create an array in the JSON file

source

pub fn set_object(&mut self, path: &Path)

Create an object in the JSON file

source

pub fn set_number(&mut self, path: &Path, value: f64)

Insert a number in the JSON file

source

pub fn set_string(&mut self, path: &Path, value: ArcStr)

Insert a string in the JSON file

source

pub fn set_boolean(&mut self, path: &Path, value: bool)

Insert a boolean in the JSON file

source

pub fn push(&mut self, path: Path) -> Path

Add an item to an array from the JSON file

The returned Path leads to the new item.

Caution: this panics if the specified path leads to a value which isn’t a Value::Array

source

pub fn iter_array(&self, base_path: &Path) -> ArrayIter<'_>

Iterates on the items in an array

Yields zero item if the value at base_path is not an array.

source

pub fn prop(&mut self, path: Path, key: &str) -> Path

Add a property to an object from the JSON file

The returned Path leads to the new property. Has no effect no the file if the property already exists.

Caution: this panics if the specified path leads to a value which isn’t a Value::Object

source

pub fn dump_to<T>(&self, f: &mut T, path: &Path) -> Result<(), Error>where T: Write,

Writes some part of this JSON file to a fmt::Write implementor

source

pub fn dump(&self, path: &Path) -> Result<ArcStr, Error>

Writes some part of this JSON file to a [ArcStr]

Trait Implementations§

source§

impl Display for JsonFile

Dumps the whole JSON File

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Index<&Path> for JsonFile

§

type Output = Value

The returned type after indexing.
source§

fn index(&self, path: &Path) -> &<JsonFile as Index<&Path>>::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<&mut Path> for JsonFile

§

type Output = Value

The returned type after indexing.
source§

fn index(&self, path: &mut Path) -> &<JsonFile as Index<&mut Path>>::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<Path> for JsonFile

§

type Output = Value

The returned type after indexing.
source§

fn index(&self, path: Path) -> &<JsonFile as Index<Path>>::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<'a, I, T> Index<T> for JsonFilewhere I: Into<PathStep<'a>>, T: IntoIterator<Item = I>,

§

type Output = Value

The returned type after indexing.
source§

fn index(&self, iter: T) -> &<JsonFile as Index<T>>::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.