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