row only.Expand description
§Convert Rust structs with named fields into tables of output
Django produces JSON encoded tabular output in response to queries, where each row of the output is a JSON object, and those objects do not contain any nested objects. Instead, nested objects are represented by a pre-selected foreign key.
§Overview
The IntoRow trait in this module allows the type to produce a Serializer that
can convert an instance into a row within a table of JSON output.
The IntoRow derive macro for this
trait will produce everything necessary for structs with
named fields.
The value of a cell within the table cannot itself be JSON
object-typed, in line with Django’s database origins. However, it
is possible to return multiple values in an array inside one
cell. The CellValue type captures the subset of permitted JSON
Values that are permitted.
A type which can directly be the value in a given cell of output
should implement IntoCellValue. If the type implements
Display, and if the desired JSON representation is a string,
you can derive the marker trait StringCellValue to benefit
from a blanket derivation of IntoCellValue for string
values. Otherwise IntoCellValue must be implemented directly.
Since Django results do not contain nested objects, fields with
structured types must be handled differently. Here, one field of
the nested type is chosen to represent that value, as what is in
effect a foreign key. The AsForeignKey trait captures this
idea, and is blanket implemented for IntoRow types.
Example:
use django_query::row::{Serializer, IntoRow};
use serde_json::json;
use std::sync::Arc;
#[derive(IntoRow)]
struct Foo {
a: i32,
#[django(foreign_key="a")]
b: Option<Arc<Foo>>
}
let f1 = Arc::new(Foo { a: 1, b: None });
let f2 = Arc::new(Foo { a: 2, b: Some(f1.clone()) });
let f3 = Arc::new(Foo { a: 3, b: Some(f2.clone()) });
let ser = Arc::<Foo>::get_serializer();
assert_eq!(ser.to_json(&f1), json! {
{ "a": 1i32, "b": null }
});
assert_eq!(ser.to_json(&f2), json! {
{ "a": 2i32, "b": 1i32 }
});
assert_eq!(ser.to_json(&f3), json! {
{ "a": 3i32, "b": 2i32 }
});Enums§
- Cell
Value - The JSON types Django permits in cells - everything except object.
Traits§
- AsForeign
Key - Convert a structured value into a single representative
CellValue - AsForeign
KeyWith Context - Convert a structured value into a single representative
CellValueusing a context value - Cell
Visitor - Visit the values in the Django output row for a given value.
- Column
Visitor - Visit the columns that a Django output row for a given type will contain.
- Into
Cell Value - Convert a value into a JSON
CellValue. - IntoRow
- Produce a
Serializerto convert values of this type into rows of output. - Into
RowWith Context - Produce a
Serializerto convert values of this type, which requires a context value, into rows of output. - Serializer
- A converter that visits rows of Django output for a given type.
- String
Cell Value - Use
Displayto convert the type into aCellValue
Derive Macros§
- IntoRow
- Derive the
IntoRowtrait, determining the display of nested objects. - Into
RowWith Persian Rug persian-rug - Derive the
IntoRowWithContexttrait forpersian-rugtypes.