@@ -49,6 +49,7 @@ use miette::Diagnostic;
49
49
use ref_cast:: RefCast ;
50
50
use smol_str:: SmolStr ;
51
51
use std:: collections:: { BTreeMap , BTreeSet , HashMap , HashSet } ;
52
+ use std:: io:: Read ;
52
53
use std:: str:: FromStr ;
53
54
54
55
/// Extended functionality for `Entities` struct
@@ -260,6 +261,81 @@ impl Entity {
260
261
ancestors. into_iter ( ) . map ( EntityUid :: new) . collect ( ) ,
261
262
)
262
263
}
264
+
265
+ /// Parse an entity from an in-memory JSON value
266
+ /// If a schema is provided, it is handled identically to [`Entities::from_json_str`]
267
+ pub fn from_json_value (
268
+ value : serde_json:: Value ,
269
+ schema : Option < & Schema > ,
270
+ ) -> Result < Self , EntitiesError > {
271
+ let schema = schema. map ( |s| cedar_policy_validator:: CoreSchema :: new ( & s. 0 ) ) ;
272
+ let eparser = cedar_policy_core:: entities:: EntityJsonParser :: new (
273
+ schema. as_ref ( ) ,
274
+ Extensions :: all_available ( ) ,
275
+ cedar_policy_core:: entities:: TCComputation :: ComputeNow ,
276
+ ) ;
277
+ eparser. single_from_json_value ( value) . map ( Self )
278
+ }
279
+
280
+ /// Parse an entity from a JSON string
281
+ /// If a schema is provided, it is handled identically to [`Entities::from_json_str`]
282
+ pub fn from_json_str (
283
+ src : impl AsRef < str > ,
284
+ schema : Option < & Schema > ,
285
+ ) -> Result < Self , EntitiesError > {
286
+ let schema = schema. map ( |s| cedar_policy_validator:: CoreSchema :: new ( & s. 0 ) ) ;
287
+ let eparser = cedar_policy_core:: entities:: EntityJsonParser :: new (
288
+ schema. as_ref ( ) ,
289
+ Extensions :: all_available ( ) ,
290
+ cedar_policy_core:: entities:: TCComputation :: ComputeNow ,
291
+ ) ;
292
+ eparser. single_from_json_str ( src) . map ( Self )
293
+ }
294
+
295
+ /// Parse an entity from a JSON reader
296
+ /// If a schema is provided, it is handled identically to [`Entities::from_json_str`]
297
+ pub fn from_json_file ( f : impl Read , schema : Option < & Schema > ) -> Result < Self , EntitiesError > {
298
+ let schema = schema. map ( |s| cedar_policy_validator:: CoreSchema :: new ( & s. 0 ) ) ;
299
+ let eparser = cedar_policy_core:: entities:: EntityJsonParser :: new (
300
+ schema. as_ref ( ) ,
301
+ Extensions :: all_available ( ) ,
302
+ cedar_policy_core:: entities:: TCComputation :: ComputeNow ,
303
+ ) ;
304
+ eparser. single_from_json_file ( f) . map ( Self )
305
+ }
306
+
307
+ /// Dump an `Entity` object into an entity JSON file.
308
+ ///
309
+ /// The resulting JSON will be suitable for parsing in via
310
+ /// `from_json_*`, and will be parse-able even with no [`Schema`].
311
+ ///
312
+ /// To read an `Entity` object from JSON , use
313
+ /// [`Self::from_json_file`], [`Self::from_json_value`], or [`Self::from_json_str`].
314
+ pub fn write_to_json ( & self , f : impl std:: io:: Write ) -> Result < ( ) , EntitiesError > {
315
+ self . 0 . write_to_json ( f)
316
+ }
317
+
318
+ /// Dump an `Entity` object into an in-memory JSON object.
319
+ ///
320
+ /// The resulting JSON will be suitable for parsing in via
321
+ /// `from_json_*`, and will be parse-able even with no `Schema`.
322
+ ///
323
+ /// To read an `Entity` object from JSON , use
324
+ /// [`Self::from_json_file`], [`Self::from_json_value`], or [`Self::from_json_str`].
325
+ pub fn to_json_value ( & self ) -> Result < serde_json:: Value , EntitiesError > {
326
+ self . 0 . to_json_value ( )
327
+ }
328
+
329
+ /// Dump an `Entity` object into a JSON string.
330
+ ///
331
+ /// The resulting JSON will be suitable for parsing in via
332
+ /// `from_json_*`, and will be parse-able even with no `Schema`.
333
+ ///
334
+ /// To read an `Entity` object from JSON , use
335
+ /// [`Self::from_json_file`], [`Self::from_json_value`], or [`Self::from_json_str`].
336
+ pub fn to_json_string ( & self ) -> Result < String , EntitiesError > {
337
+ self . 0 . to_json_string ( )
338
+ }
263
339
}
264
340
265
341
impl std:: fmt:: Display for Entity {
0 commit comments