Serde Java Properties

Docs License Version

Java Properties is a simple, line-oriented format for specifying key-value resources used in Java programs. This crate offers basic (de-)serializers for use with serde-enabled datastructures.

properties field_a: a value field_b: 100 field_c: true

Implementation

Internally, the java-properties crate is used for iterating key-value pairs in an input stream, and writing key-value pairs to an output stream.

Deserializing a struct

Usually, the format is untyped i.e. it deserialises to a map from String to String. This crate uses the default std::str::FromStr implementations for integers, floats and booleans to provide a typed interface on top of that. That way, simple structures or maps that implement serde::Deserialize can be loaded from properties files.

```rs use serde::Deserialize;

[derive(Debug, PartialEq, Deserialize)]

struct Data { fielda: String, fieldb: usize, fieldc: bool, } let text = " fielda: a value fieldb: 100 fieldc: true ";

let data: Data = serdejavaproperties::from_str(text).unwrap();

asserteq!(data.fielda, "a value"); asserteq!(data.fieldb, 100); asserteq!(data.fieldc, true); ```

Serializing a struct

Serialization uses the default std::fmt::Display implementations for each primitive type.

Supported in the top-level Serializer: - Maps - Structs - Enums of struct variants - Options of all of these

Supported in the field-level Serializer: - Integers (i8, i16, i32, i64, u8, u16, u32, u64) - Floats (f32, f64) - Booleans (true or false) - Strings - Enums of unit variants - Options of all of these

```rs use serde::Serialize;

[derive(Debug, PartialEq, Serialize)]

struct Data { fielda: String, fieldb: usize, field_c: bool, }

let data = Data { fielda: "value".tostring(), fieldb: 100, fieldc: true }; let string = serdejavaproperties::to_string(&data).unwrap();

asserteq!(string, "fielda=value\nfieldb=100\nfieldc=true\n"); ```

Alternatives

Similar to the java-properties crate itself, this crate is supposed to be an exact match to the format as specified in Java. If you need a more powerful configuration syntax, that supports nested structs, you should probably use HOCON.