JsonTable

Struct JsonTable 

Source
pub struct JsonTable<T> { /* private fields */ }
Expand description

Converter of Value to a table, with a set of configurations.

Implementations§

Source§

impl<T> JsonTable<T>

Source

pub fn new(value: T) -> Self

Creates a new JsonTable object.

use serde_json::json;
use json_to_table::JsonTable;

let value = json!([
    123,
    ["123", "234", "456"],
    {"k1": 1, "k2": 2},
]);

let table = JsonTable::new(&value).to_string();

assert_eq!(
    table,
    concat!(
        "+--------------+\n",
        "|  123         |\n",
        "+--------------+\n",
        "| +-------+    |\n",
        "| |  123  |    |\n",
        "| +-------+    |\n",
        "| |  234  |    |\n",
        "| +-------+    |\n",
        "| |  456  |    |\n",
        "| +-------+    |\n",
        "+--------------+\n",
        "| +----+-----+ |\n",
        "| | k1 |  1  | |\n",
        "| +----+-----+ |\n",
        "| | k2 |  2  | |\n",
        "| +----+-----+ |\n",
        "+--------------+",
    ),
);
Source

pub fn collapse(&mut self) -> &mut Self

Collapse tables out instead of tables within tables.

use serde_json::json;
use json_to_table::json_to_table;

let value = json!({
    "key1": 123,
    "234": ["123", "234", "456"],
    "key22": {
        "k1": 1,
        "k2": 2,
    }
});

let table = json_to_table(&value)
    .collapse()
    .to_string();

assert_eq!(
    table,
    concat!(
        "+-------+--------+\n",
        "| 234   | 123    |\n",
        "|       +--------+\n",
        "|       | 234    |\n",
        "|       +--------+\n",
        "|       | 456    |\n",
        "+-------+--------+\n",
        "| key1  | 123    |\n",
        "+-------+----+---+\n",
        "| key22 | k1 | 1 |\n",
        "|       +----+---+\n",
        "|       | k2 | 2 |\n",
        "+-------+----+---+",
    ),
);
Source

pub fn object_orientation(&mut self, mode: Orientation) -> &mut Self

Set a table mode for a serde_json::Value::Object.

use serde_json::json;
use json_to_table::{json_to_table, Orientation};

let value = json!({
    "key1": 123,
    "234": ["123", "234", "456"],
    "key22": {
        "k1": 1,
        "k2": 2,
    }
});

let table = json_to_table(&value)
    .object_orientation(Orientation::Row)
    .to_string();

assert_eq!(
    table,
    concat!(
        "+-----------+-------+---------------+\n",
        "| 234       | key1  | key22         |\n",
        "+-----------+-------+---------------+\n",
        "| +-------+ |  123  | +-----+-----+ |\n",
        "| |  123  | |       | | k1  | k2  | |\n",
        "| +-------+ |       | +-----+-----+ |\n",
        "| |  234  | |       | |  1  |  2  | |\n",
        "| +-------+ |       | +-----+-----+ |\n",
        "| |  456  | |       |               |\n",
        "| +-------+ |       |               |\n",
        "+-----------+-------+---------------+",
    ),
);

let table = json_to_table(&value)
    .object_orientation(Orientation::Column)
    .to_string();

assert_eq!(
    table,
    concat!(
        "+-------+--------------+\n",
        "| 234   | +-------+    |\n",
        "|       | |  123  |    |\n",
        "|       | +-------+    |\n",
        "|       | |  234  |    |\n",
        "|       | +-------+    |\n",
        "|       | |  456  |    |\n",
        "|       | +-------+    |\n",
        "+-------+--------------+\n",
        "| key1  |  123         |\n",
        "+-------+--------------+\n",
        "| key22 | +----+-----+ |\n",
        "|       | | k1 |  1  | |\n",
        "|       | +----+-----+ |\n",
        "|       | | k2 |  2  | |\n",
        "|       | +----+-----+ |\n",
        "+-------+--------------+",
    ),
);
Source

pub fn array_orientation(&mut self, mode: Orientation) -> &mut Self

Set a table mode for a serde_json::Value::Array.

use serde_json::json;
use json_to_table::{json_to_table, Orientation};

let value = json!({
    "key1": 123,
    "234": ["123", "234", "456"],
    "key22": {
        "k1": 1,
        "k2": 2,
    }
});

let table = json_to_table(&value)
    .array_orientation(Orientation::Row)
    .to_string();

assert_eq!(
    table,
    concat!(
        "+-------+---------------------------+\n",
        "| 234   | +-------+-------+-------+ |\n",
        "|       | |  123  |  234  |  456  | |\n",
        "|       | +-------+-------+-------+ |\n",
        "+-------+---------------------------+\n",
        "| key1  |  123                      |\n",
        "+-------+---------------------------+\n",
        "| key22 | +----+-----+              |\n",
        "|       | | k1 |  1  |              |\n",
        "|       | +----+-----+              |\n",
        "|       | | k2 |  2  |              |\n",
        "|       | +----+-----+              |\n",
        "+-------+---------------------------+",
    ),
);

let table = json_to_table(&value)
    .array_orientation(Orientation::Column)
    .to_string();

assert_eq!(
    table,
    concat!(
        "+-------+--------------+\n",
        "| 234   | +-------+    |\n",
        "|       | |  123  |    |\n",
        "|       | +-------+    |\n",
        "|       | |  234  |    |\n",
        "|       | +-------+    |\n",
        "|       | |  456  |    |\n",
        "|       | +-------+    |\n",
        "+-------+--------------+\n",
        "| key1  |  123         |\n",
        "+-------+--------------+\n",
        "| key22 | +----+-----+ |\n",
        "|       | | k1 |  1  | |\n",
        "|       | +----+-----+ |\n",
        "|       | | k2 |  2  | |\n",
        "|       | +----+-----+ |\n",
        "+-------+--------------+",
    ),
);
Source

pub fn with<O>(&mut self, option: O) -> &mut Self

Set a config which will be used.

You can obtain a config from a Table.

§Example
use serde_json::json;
use json_to_table::json_to_table;
use tabled::{
    settings::{Alignment, Padding, Style},
    Table
};

let value = json!({
    "key1": 123,
    "234": ["123", "234", "456"],
    "key22": {
        "k1": 1,
        "k2": 2,
    }
});

let table = json_to_table(&value)
    .with(Padding::zero())
    .with(Alignment::right())
    .with(Style::extended())
    .collapse()
    .to_string();

assert_eq!(
    table,
    concat!(
         "╔═════╦════╗\n",  
         "║  234║ 123║\n",  
         "║     ╠════╣\n",  
         "║     ║ 234║\n",  
         "║     ╠════╣\n",  
         "║     ║ 456║\n",  
         "╠═════╬════╣\n",  
         "║ key1║ 123║\n",  
         "╠═════╬══╦═╣\n",  
         "║key22║k1║1║\n",  
         "║     ╠══╬═╣\n",  
         "║     ║k2║2║\n",  
         "╚═════╩══╩═╝",
    ),
);
Source

pub fn into_table(&self) -> Table
where T: Borrow<Value>,

Convert the table into a Table.

It does not recognizes collapsed mode.

use tabled::settings::style::Style;
use json_to_table::Orientation;

let json = serde_json::json!({
    "key1": "value1",
    "key2": {
        "key1": 123,
        "key2": [1, 2, 3, 4, 5],
    },
    "key3": [
        {"key": 123.3},
        2,
        "asd"
    ],
    "key4": 1234.567
});

let table = json_to_table::json_to_table(&json)
    .with(Style::modern())
    .array_orientation(Orientation::Row)
    .into_table()
    .with(Style::markdown())
    .to_string();

assert_eq!(
    table,
    concat!(
        "| key1 | value1                                     |\n",
        "|------|--------------------------------------------|\n",
        "| key2 | ┌──────┬─────────────────────────────────┐ |\n",
        "|      | │ key1 │  123                            │ |\n",
        "|      | ├──────┼─────────────────────────────────┤ |\n",
        "|      | │ key2 │ ┌─────┬─────┬─────┬─────┬─────┐ │ |\n",
        "|      | │      │ │  1  │  2  │  3  │  4  │  5  │ │ |\n",
        "|      | │      │ └─────┴─────┴─────┴─────┴─────┘ │ |\n",
        "|      | └──────┴─────────────────────────────────┘ |\n",
        "| key3 | ┌───────────────────┬─────┬───────┐        |\n",
        "|      | │ ┌─────┬─────────┐ │  2  │  asd  │        |\n",
        "|      | │ │ key │  123.3  │ │     │       │        |\n",
        "|      | │ └─────┴─────────┘ │     │       │        |\n",
        "|      | └───────────────────┴─────┴───────┘        |\n",
        "| key4 | 1234.567                                   |",
    ),
)

Though it’s said it’s not suppose to work with collapsed tables; It does; Though it’s a bit different.

use tabled::settings::style::Style;
use json_to_table::Orientation;

let json = serde_json::json!({
    "key1": "value1",
    "key2": {
        "key1": 123,
        "key2": [1, 2, 3, 4, 5],
    },
    "key3": [
        {"key": 123.3},
        2,
        "asd"
    ],
    "key4": 1234.567
});

let table = json_to_table::json_to_table(&json)
    .with(Style::modern())
    .array_orientation(Orientation::Row)
    .into_table()
    .with(Style::ascii_rounded())
    .to_string();

assert_eq!(
    table,
    concat!(
        ".---------------------------------------------------.\n",
        "| key1 | value1                                     |\n",
        "| key2 | ┌──────┬─────────────────────────────────┐ |\n",
        "|      | │ key1 │  123                            │ |\n",
        "|      | ├──────┼─────────────────────────────────┤ |\n",
        "|      | │ key2 │ ┌─────┬─────┬─────┬─────┬─────┐ │ |\n",
        "|      | │      │ │  1  │  2  │  3  │  4  │  5  │ │ |\n",
        "|      | │      │ └─────┴─────┴─────┴─────┴─────┘ │ |\n",
        "|      | └──────┴─────────────────────────────────┘ |\n",
        "| key3 | ┌───────────────────┬─────┬───────┐        |\n",
        "|      | │ ┌─────┬─────────┐ │  2  │  asd  │        |\n",
        "|      | │ │ key │  123.3  │ │     │       │        |\n",
        "|      | │ └─────┴─────────┘ │     │       │        |\n",
        "|      | └───────────────────┴─────┴───────┘        |\n",
        "| key4 | 1234.567                                   |\n",
        "'---------------------------------------------------'",
    ),
)
Source

pub fn into_pool_table(&self) -> PoolTable
where T: Borrow<Value>,

Convert the table into a PoolTable.

It recognizes only collapsed mode.

let json = serde_json::json!({
    "key1": "value1",
    "key2": {
        "key1": 123,
        "key2": [1, 2, 3, 4, 5],
    },
    "key3": [
        {"key": 123.3},
        2,
        "asd"
    ],
    "key4": 1234.567
});

let table = json_to_table::json_to_table(&json)
    .into_pool_table()
    .to_string();

assert_eq!(
    table,
    concat!(
       "+--------+-----------+\n",
       "| key1   | \"value1\"  |\n",
       "+-------++-----+-----+\n",
       "| key2  | key1 | 123 |\n",
       "|       +------++----+\n",
       "|       | key2  | 1  |\n",
       "|       |       +----+\n",
       "|       |       | 2  |\n",
       "|       |       +----+\n",
       "|       |       | 3  |\n",
       "|       |       +----+\n",
       "|       |       | 4  |\n",
       "|       |       +----+\n",
       "|       |       | 5  |\n",
       "+------++----+--+----+\n",
       "| key3 | key | 123.3 |\n",
       "|      +-----+-------+\n",
       "|      | 2           |\n",
       "|      +-------------+\n",
       "|      | \"asd\"       |\n",
       "+------+-+-----------+\n",
       "| key4   | 1234.567  |\n",
       "+--------+-----------+",
    ),
)

Trait Implementations§

Source§

impl<T: Clone> Clone for JsonTable<T>

Source§

fn clone(&self) -> JsonTable<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for JsonTable<T>

Source§

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

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

impl<T> Display for JsonTable<T>
where T: Borrow<Value>,

Source§

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

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

impl<T> From<JsonTable<T>> for PoolTable
where T: Borrow<Value>,

Source§

fn from(val: JsonTable<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<JsonTable<T>> for Table
where T: Borrow<Value>,

Source§

fn from(val: JsonTable<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for JsonTable<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for JsonTable<T>
where T: RefUnwindSafe,

§

impl<T> Send for JsonTable<T>
where T: Send,

§

impl<T> Sync for JsonTable<T>
where T: Sync,

§

impl<T> Unpin for JsonTable<T>
where T: Unpin,

§

impl<T> UnwindSafe for JsonTable<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

fn to_string(&self) -> String

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

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.