-
Notifications
You must be signed in to change notification settings - Fork 107
Adding read/write json methods to entity #924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,6 +263,8 @@ impl Entity { | |
} | ||
|
||
/// Parse an entity from an in-memory JSON value | ||
/// If a schema is provided, it is handled identically to | ||
/// [Entities](https://docs.rs/cedar-policy/latest/cedar_policy/struct.Entities.html#method.from_json_str) | ||
|
||
pub fn from_json_value( | ||
value: serde_json::Value, | ||
schema: Option<&Schema>, | ||
|
@@ -277,6 +279,8 @@ impl Entity { | |
} | ||
|
||
/// Parse an entity from a JSON string | ||
/// If a schema is provided, it is handled identically to | ||
/// [Entities](https://docs.rs/cedar-policy/latest/cedar_policy/struct.Entities.html#method.from_json_str) | ||
pub fn from_json_str( | ||
src: impl AsRef<str>, | ||
schema: Option<&Schema>, | ||
|
@@ -291,6 +295,8 @@ impl Entity { | |
} | ||
|
||
/// Parse an entity from a JSON reader | ||
/// If a schema is provided, it is handled identically to | ||
/// [Entities](https://docs.rs/cedar-policy/latest/cedar_policy/struct.Entities.html#method.from_json_str) | ||
pub fn from_json_file(f: impl Read, schema: Option<&Schema>) -> Result<Self, EntitiesError> { | ||
let schema = schema.map(|s| cedar_policy_validator::CoreSchema::new(&s.0)); | ||
let eparser = cedar_policy_core::entities::EntityJsonParser::new( | ||
|
@@ -306,11 +312,33 @@ impl Entity { | |
/// The resulting JSON will be suitable for parsing in via | ||
/// `from_json_*`, and will be parse-able even with no [`Schema`]. | ||
/// | ||
/// To read an `Entity` object from an entities JSON file, use | ||
/// [`Entity::from_json_file`]. | ||
/// To read an `Entity` object from JSON , use | ||
/// [`from_json_file`], [`from_json_value`], or [`from_json_str`]. | ||
pub fn write_to_json(&self, f: impl std::io::Write) -> Result<(), EntitiesError> { | ||
self.0.write_to_json(f) | ||
} | ||
|
||
/// Dump an `Entity` object into an in-memory JSON object. | ||
/// | ||
/// The resulting JSON will be suitable for parsing in via | ||
/// `from_json_*`, and will be parse-able even with no `Schema`. | ||
/// | ||
/// To read an `Entity` object from JSON , use | ||
/// [`from_json_file`], [`from_json_value`], or [`from_json_str`]. | ||
pub fn to_json_value(&self) -> Result<serde_json::Value, EntitiesError> { | ||
self.0.to_json_value() | ||
} | ||
|
||
/// Dump an `Entity` object into a JSON string. | ||
/// | ||
/// The resulting JSON will be suitable for parsing in via | ||
/// `from_json_*`, and will be parse-able even with no `Schema`. | ||
/// | ||
/// To read an `Entity` object from JSON , use | ||
/// [`from_json_file`], [`from_json_value`], or [`from_json_str`]. | ||
pub fn to_json_string(&self) -> Result<String, EntitiesError> { | ||
self.0.to_json_string() | ||
} | ||
} | ||
|
||
impl std::fmt::Display for Entity { | ||
|
Uh oh!
There was an error while loading. Please reload this page.