Skip to main content

encode_record

Function encode_record 

Source
pub fn encode_record(
    schema: &Schema,
    json: &Value,
    options: &EncodeOptions,
) -> Result<Vec<u8>>
Expand description

Encode JSON data to binary using the provided schema

§Arguments

  • schema - The parsed copybook schema
  • json - The JSON data to encode
  • options - Encoding options

§Examples

use copybook_core::parse_copybook;
use copybook_codec::{encode_record, EncodeOptions};
use copybook_codec::options::{Codepage, RecordFormat};
use serde_json::json;

let schema = parse_copybook("01 FLD PIC X(5).").unwrap();
let json = json!({"fields": {"FLD": "HELLO"}});
let options = EncodeOptions::new()
    .with_codepage(Codepage::ASCII)
    .with_format(RecordFormat::Fixed);
let binary = encode_record(&schema, &json, &options).unwrap();
assert_eq!(&binary[..5], b"HELLO");

§Errors

Returns an error if the JSON data cannot be encoded according to the schema.