Skip to main content

from_json

Function from_json 

Source
pub fn from_json(
    json: Document<'_>,
    version: Version,
) -> Caveat<Versioned<'_>, Warning>
Expand description

Validate a json::Document against the OCPI CDR schema for the given [Version]12.

The json::Document is obtained by calling json::parse_object. Any unexpected, missing, or wrongly typed fields are reported as a warning::Set of schema::Warnings carried by the returned Caveat.

§Example


let doc = json::parse_object(CDR_JSON)?;
let (cdr, warnings) = cdr::from_json(doc, Version::V211).into_parts();

if !warnings.is_empty() {
    eprintln!("The CDR has `{}` schema warnings.", warnings.len_warnings());
}
Examples found in repository?
examples/price_cdr_with_known_version.rs (line 13)
7fn main() {
8    const CDR_JSON: &str = include_str!("data/cdr_time_and_parking_time.json");
9
10    // If you know the version of a CDR you can parse it into a `json::Document` and validate it
11    // against the schema for that version.
12    let doc = json::parse_object(CDR_JSON).expect("unable to parse CDR");
13    let (cdr, warnings) = cdr::from_json(doc, Version::V211).into_parts();
14
15    if !warnings.is_empty() {
16        eprintln!("The CDR has `{}` schema warnings.", warnings.len_warnings());
17    }
18
19    let report = match cdr::price(&cdr, price::TariffSource::UseCdr, Tz::Europe__Amsterdam) {
20        Ok(r) => r,
21        Err(set) => {
22            let (error, warnings) = set.into_parts();
23            print_pricing_error(&error);
24            print_pricing_warnings(&warnings);
25            return;
26        }
27    };
28
29    let (report, warnings) = report.into_parts();
30    print_pricing_warnings(&warnings);
31
32    // The various fields of the `price::Report` can be examined or converted to JSON.
33    let price::Report {
34        periods: _,
35        tariff_used: _,
36        tariff_reports: _,
37        timezone: _,
38        billed_energy: _,
39        billed_idle_time: _,
40        total_charging_time: _,
41        billed_charging_time: _,
42        total_cost: _,
43        total_fixed_cost: _,
44        total_time: _,
45        total_charging_time_cost: _,
46        total_energy: _,
47        total_energy_cost: _,
48        total_idle_time: _,
49        total_idle_cost: _,
50    } = report;
51}