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>
impl<T> JsonTable<T>
Sourcepub fn new(value: T) -> Self
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",
"+--------------+",
),
);Sourcepub fn collapse(&mut self) -> &mut Self
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",
"+-------+----+---+",
),
);Sourcepub fn object_orientation(&mut self, mode: Orientation) -> &mut Self
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",
"+-------+--------------+",
),
);Sourcepub fn array_orientation(&mut self, mode: Orientation) -> &mut Self
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",
"+-------+--------------+",
),
);Sourcepub fn with<O>(&mut self, option: O) -> &mut Self
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",
"╚═════╩══╩═╝",
),
);Sourcepub fn into_table(&self) -> Table
pub fn into_table(&self) -> Table
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",
"'---------------------------------------------------'",
),
)Sourcepub fn into_pool_table(&self) -> PoolTable
pub fn into_pool_table(&self) -> PoolTable
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more