1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! The JsonString type is defined here. It is used throughout Holochain
//! to enforce a standardized serialization of data to/from json.

use crate::error::{JsonError, JsonResult};
use serde::{de::DeserializeOwned, Serialize};
use serde_json;
use std::{
    convert::{TryFrom, TryInto},
    fmt::{Debug, Display, Formatter, Result as FmtResult},
};

/// track json serialization with the rust type system!
/// JsonString wraps a string containing JSON serialized data
/// avoid accidental double-serialization or forgetting to serialize
/// serialize any type consistently including hard-to-reach places like Option<Entry> and Result
/// JsonString must not itself be serialized/deserialized
/// instead, implement and use the native `From` trait to move between types
/// - moving to/from String, str, JsonString and JsonString simply (un)wraps it as raw JSON data
/// - moving to/from any other type must offer a reliable serialization/deserialization strategy
#[derive(Debug, PartialEq, Clone, Hash, Eq, Serialize, Deserialize)]
pub struct JsonString(String);

impl JsonString {
    /// a null JSON value
    /// e.g. represents None when implementing From<Option<Foo>>
    pub fn null() -> JsonString {
        JsonString::from_json("null")
    }

    pub fn empty_object() -> JsonString {
        JsonString::from_json("{}")
    }

    pub fn is_null(&self) -> bool {
        self == &Self::null()
    }

    /// achieves the same outcome as serde_json::to_vec()
    pub fn to_bytes(&self) -> Vec<u8> {
        self.0.as_bytes().to_vec()
    }

    // Creates a JsonString from stringified json
    // replaces From<String> for JsonString and requires conversions to be explicit
    // This is because string types must be handled differently depending on if
    // they are strinfigied JSON or JSON strings
    pub fn from_json(s: &str) -> JsonString {
        let cleaned = s
            // remove whitespace from both ends
            .trim()
            // remove null characters from both ends
            .trim_matches(char::from(0));
        JsonString(cleaned.to_owned())
    }
}

impl From<bool> for JsonString {
    fn from(u: bool) -> JsonString {
        default_to_json(u)
    }
}

impl From<u32> for JsonString {
    fn from(u: u32) -> JsonString {
        default_to_json(u)
    }
}

impl From<i32> for JsonString {
    fn from(u: i32) -> JsonString {
        default_to_json(u)
    }
}

impl From<u64> for JsonString {
    fn from(u: u64) -> JsonString {
        default_to_json(u)
    }
}

impl From<u128> for JsonString {
    fn from(u: u128) -> JsonString {
        default_to_json(u)
    }
}

impl TryFrom<JsonString> for bool {
    type Error = JsonError;
    fn try_from(j: JsonString) -> Result<Self, Self::Error> {
        default_try_from_json(j)
    }
}

impl TryFrom<JsonString> for u32 {
    type Error = JsonError;
    fn try_from(j: JsonString) -> Result<Self, Self::Error> {
        default_try_from_json(j)
    }
}

impl TryFrom<JsonString> for u64 {
    type Error = JsonError;
    fn try_from(j: JsonString) -> Result<Self, Self::Error> {
        default_try_from_json(j)
    }
}

impl From<serde_json::Value> for JsonString {
    fn from(v: serde_json::Value) -> JsonString {
        JsonString::from_json(&v.to_string())
    }
}

impl From<JsonString> for String {
    fn from(json_string: JsonString) -> String {
        json_string.0
    }
}

impl<'a> From<&'a JsonString> for &'a str {
    fn from(json_string: &'a JsonString) -> &'a str {
        &json_string.0
    }
}

impl<'a> From<&'a JsonString> for String {
    fn from(json_string: &JsonString) -> String {
        String::from(json_string.to_owned())
    }
}

impl From<&'static str> for JsonString {
    fn from(s: &str) -> JsonString {
        JsonString::from_json(&String::from(s))
    }
}

impl<T: Serialize> From<Vec<T>> for JsonString {
    fn from(vector: Vec<T>) -> JsonString {
        JsonString::from_json(&serde_json::to_string(&vector).expect("could not Jsonify vector"))
    }
}

// conversions from result types

fn result_to_json_string<T: Into<JsonString>, E: Into<JsonString>>(
    result: Result<T, E>,
) -> JsonString {
    let is_ok = result.is_ok();
    let inner_json: JsonString = match result {
        Ok(inner) => inner.into(),
        Err(inner) => inner.into(),
    };
    let inner_string = String::from(inner_json);
    JsonString::from_json(&format!(
        "{{\"{}\":{}}}",
        if is_ok { "Ok" } else { "Err" },
        inner_string
    ))
}

impl<T, E> From<Result<T, E>> for JsonString
where
    T: Into<JsonString>,
    E: Into<JsonString>,
{
    fn from(result: Result<T, E>) -> JsonString {
        result_to_json_string(result)
    }
}

impl<T> From<Result<T, String>> for JsonString
where
    T: Into<JsonString>,
{
    fn from(result: Result<T, String>) -> JsonString {
        result_to_json_string(result.map_err(|e| RawString::from(e)))
    }
}

impl<E> From<Result<String, E>> for JsonString
where
    E: Into<JsonString>,
{
    fn from(result: Result<String, E>) -> JsonString {
        result_to_json_string(result.map(|v| RawString::from(v)))
    }
}

impl From<Result<String, String>> for JsonString {
    fn from(result: Result<String, String>) -> JsonString {
        result_to_json_string(
            result
                .map(|v| RawString::from(v))
                .map_err(|e| RawString::from(e)),
        )
    }
}

// conversions to result types

impl<T, E> TryInto<Result<T, E>> for JsonString
where
    T: Into<JsonString> + DeserializeOwned,
    E: Into<JsonString> + DeserializeOwned,
{
    type Error = JsonError;
    fn try_into(self) -> Result<Result<T, E>, Self::Error> {
        default_try_from_json(self)
    }
}

impl<T> TryInto<Result<T, String>> for JsonString
where
    T: Into<JsonString> + DeserializeOwned,
{
    type Error = JsonError;
    fn try_into(self) -> Result<Result<T, String>, Self::Error> {
        default_try_from_json(self)
    }
}

impl<E> TryInto<Result<String, E>> for JsonString
where
    E: Into<JsonString> + DeserializeOwned,
{
    type Error = JsonError;
    fn try_into(self) -> Result<Result<String, E>, Self::Error> {
        default_try_from_json(self)
    }
}

impl TryInto<Result<String, String>> for JsonString {
    type Error = JsonError;
    fn try_into(self) -> Result<Result<String, String>, Self::Error> {
        default_try_from_json(self)
    }
}

impl From<()> for JsonString {
    fn from(_: ()) -> Self {
        default_to_json(())
    }
}

impl TryFrom<JsonString> for () {
    type Error = JsonError;
    fn try_from(j: JsonString) -> Result<Self, Self::Error> {
        default_try_from_json(j)
    }
}

impl Display for JsonString {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "{}", String::from(self),)
    }
}

// ## Conversions from Option types ##

// Options are a special case for several reasons. Firstly they are handled
// in a special way by serde:

//     "Users tend to have different expectations around the Option enum compared to other enums.
//     Serde JSON will serialize Option::None as null and Option::Some as just the contained value."

// The other issue is that option implements generic From<T> so you can do calls like
// `let s: Option<&str> = "hi".into()`
// To get around this we need to go through an intermediate type JsonStringOption

#[derive(Shrinkwrap, Deserialize)]
pub struct JsonStringOption<T>(Option<T>);

impl<T> JsonStringOption<T> {
    pub fn to_option(self) -> Option<T> {
        self.0
    }
}

impl<T> Into<Option<T>> for JsonStringOption<T> {
    fn into(self) -> Option<T> {
        self.to_option()
    }
}

impl<T> TryInto<JsonStringOption<T>> for JsonString
where
    T: Into<JsonString> + DeserializeOwned,
{
    type Error = JsonError;
    fn try_into(self) -> Result<JsonStringOption<T>, Self::Error> {
        let o: Option<T> = default_try_from_json(self)?;
        Ok(JsonStringOption(o))
    }
}

impl TryInto<JsonStringOption<String>> for JsonString {
    type Error = JsonError;
    fn try_into(self) -> Result<JsonStringOption<String>, Self::Error> {
        let o: Option<String> = default_try_from_json(self)?;
        Ok(JsonStringOption(o))
    }
}

// conversions from options to JsonString

impl<T> From<Option<T>> for JsonString
where
    T: Debug + Serialize + Into<JsonString>,
{
    fn from(o: Option<T>) -> JsonString {
        default_to_json(o)
    }
}

impl From<Option<String>> for JsonString {
    fn from(o: Option<String>) -> JsonString {
        default_to_json(o)
    }
}

/// if all you want to do is implement the default behaviour then use #[derive(DefaultJson)]
/// should only be used with From<S> for JsonString
/// i.e. when failure should be impossible so an expect is ok
/// this is always true for serializable structs/enums
/// standard boilerplate:
/// impl From<MyStruct> for JsonString {
///     fn from(v: MyStruct) -> Self {
///         default_to_json(v)
///     }
/// }
pub fn default_to_json<V: Serialize + Debug>(v: V) -> JsonString {
    serde_json::to_string(&v)
        .map(|s| JsonString::from_json(&s))
        .map_err(|e| JsonError::SerializationError(e.to_string()))
        .unwrap_or_else(|_| panic!("could not Jsonify: {:?}", v))
}

/// if all you want to do is implement the default behaviour then use #[derive(DefaultJson)]
/// standard boilerplate should include JsonError as the Error:
/// impl TryFrom<JsonString> for T {
///     type Error = JsonError;
///     fn try_from(j: JsonString) -> JsonResult<Self> {
///         default_try_from_json(j)
///     }
/// }
pub fn default_try_from_json<D: DeserializeOwned>(json_string: JsonString) -> Result<D, JsonError> {
    serde_json::from_str(&String::from(&json_string))
        .map_err(|e| JsonError::SerializationError(e.to_string()))
}

pub trait DefaultJson:
    Serialize + DeserializeOwned + TryFrom<JsonString> + Into<JsonString>
{
}

/// generic type to facilitate Jsonifying values directly
/// JsonString simply wraps String and str as-is but will Jsonify RawString("foo") as "\"foo\""
/// RawString must not implement Serialize because it should always convert to JsonString with from
/// RawString can implement Deserialize because JsonString uses default serde to step down
#[derive(PartialEq, Debug, Clone, Deserialize)]
pub struct RawString(serde_json::Value);

impl From<&'static str> for RawString {
    fn from(s: &str) -> RawString {
        RawString(serde_json::Value::String(s.to_owned()))
    }
}

impl From<String> for RawString {
    fn from(s: String) -> RawString {
        RawString(serde_json::Value::String(s))
    }
}

impl From<f64> for RawString {
    fn from(i: f64) -> RawString {
        RawString(serde_json::Value::Number(
            serde_json::Number::from_f64(i).expect("could not accept number"),
        ))
    }
}

impl From<i32> for RawString {
    fn from(i: i32) -> RawString {
        RawString::from(f64::from(i))
    }
}

impl From<RawString> for String {
    fn from(raw_string: RawString) -> String {
        // this will panic if RawString does not contain a string!
        // use JsonString::from(...) to stringify numbers or other values
        // @see raw_from_number_test()
        String::from(raw_string.0.as_str().unwrap_or_else(|| {
            panic!(
                "could not extract inner string for RawString: {:?}",
                &raw_string
            )
        }))
    }
}

/// it should always be possible to Jsonify RawString, if not something is very wrong
impl From<RawString> for JsonString {
    fn from(raw_string: RawString) -> JsonString {
        JsonString::from_json(
            &serde_json::to_string(&raw_string.0)
                .unwrap_or_else(|_| panic!("could not Jsonify RawString: {:?}", &raw_string)),
        )
    }
}

/// converting a JsonString to RawString can fail if the JsonString is not a serialized string
impl TryFrom<JsonString> for RawString {
    type Error = JsonError;
    fn try_from(j: JsonString) -> JsonResult<Self> {
        default_try_from_json(j)
    }
}

#[cfg(test)]
pub mod tests {
    use crate::{
        error::JsonError,
        json::{JsonString, JsonStringOption, RawString},
    };
    use serde_json;
    use std::convert::{TryFrom, TryInto};

    #[derive(Serialize, Deserialize, Debug, DefaultJson, PartialEq, Clone)]
    struct DeriveTest {
        foo: String,
    }

    #[test]
    fn default_json_round_trip_test() {
        let test = DeriveTest { foo: "bar".into() };
        let expected = JsonString::from_json("{\"foo\":\"bar\"}");
        assert_eq!(expected, JsonString::from(test.clone()),);

        assert_eq!(&DeriveTest::try_from(expected).unwrap(), &test,);

        assert_eq!(
            test.clone(),
            DeriveTest::try_from(JsonString::from(test)).unwrap(),
        );
    }

    #[test]
    fn json_none_test() {
        assert_eq!(String::from("null"), String::from(JsonString::null()),);
    }

    #[test]
    fn json_into_bytes_test() {
        // note that the byte array has the quote character '/"' at the beginnging and end so it is actually valid json
        assert_eq!(
            JsonString::from(RawString::from("foo")).to_bytes(),
            vec![34, 102, 111, 111, 34],
        );
    }

    #[test]
    fn json_result_round_trip_test() {
        let result: Result<String, JsonError> = Err(JsonError::ErrorGeneric("foo".into()));

        assert_eq!(
            JsonString::from(result),
            JsonString::from_json("{\"Err\":{\"ErrorGeneric\":\"foo\"}}"),
        );

        let result: Result<String, String> = Err(String::from("foo"));

        assert_eq!(
            JsonString::from(result),
            JsonString::from_json("{\"Err\":\"foo\"}"),
        )
    }

    #[test]
    /// show From<&str> and From<String> for JsonString
    fn json_from_string_test() {
        assert_eq!(
            String::from("\"foo\""),
            String::from(JsonString::from(RawString::from("foo"))),
        );

        assert_eq!(
            String::from("\"foo\""),
            String::from(JsonString::from_json(&String::from("\"foo\""))),
        );

        assert_eq!(
            String::from("\"foo\""),
            String::from(&JsonString::from(RawString::from("foo"))),
        );
    }

    #[test]
    /// show From<serde_json::Value> for JsonString
    fn json_from_serde_test() {
        assert_eq!(
            String::from("\"foo\""),
            String::from(JsonString::from(serde_json::Value::from("foo"))),
        );
    }

    #[test]
    /// show From<Vec<T>> for JsonString
    fn json_from_vec() {
        assert_eq!(
            String::from("[\"foo\",\"bar\"]"),
            String::from(JsonString::from(vec!["foo", "bar"])),
        );
    }

    #[test]
    /// show From<&str> and From<String> for RawString
    fn raw_from_string_test() {
        assert_eq!(RawString::from(String::from("foo")), RawString::from("foo"),);
    }

    #[test]
    /// show From<RawString> for String
    fn string_from_raw_test() {
        assert_eq!(String::from("foo"), String::from(RawString::from("foo")),);
    }

    #[test]
    /// show From<RawString> for JsonString
    fn json_from_raw_test() {
        assert_eq!(
            String::from("\"foo\""),
            String::from(JsonString::from(RawString::from("foo"))),
        );
    }

    #[test]
    /// show From<JsonString> for RawString
    fn raw_from_json_test() {
        assert_eq!(
            String::from(RawString::try_from(JsonString::from("\"foo\"")).unwrap()),
            String::from("foo"),
        );
    }

    #[test]
    /// show From<number> for RawString
    fn raw_from_number_test() {
        assert_eq!(
            String::from("1.0"),
            String::from(JsonString::from(RawString::from(1))),
        );
    }

    #[test]
    fn result_from_json_string() {
        let j = JsonString::from_json(r#"{"Ok":"raw-string-content"}"#);
        let r: Result<RawString, JsonError> = j
            .try_into()
            .expect("Could not convert json string to result type");

        assert_eq!(r.unwrap(), RawString::from("raw-string-content"),);
    }

    #[test]
    fn result_from_json_string_with_strings() {
        let j = JsonString::from_json(r#"{"Ok":"string-content"}"#);
        let r: Result<String, String> = j
            .try_into()
            .expect("Could not convert json string to result type");

        assert_eq!(r.unwrap(), String::from("string-content"),);
    }

    #[test]
    fn options_are_converted_to_null_or_value_respectively() {
        let o: Option<u32> = None;
        let j: JsonString = o.into();
        assert_eq!(j, JsonString::from_json("null"));

        let o: Option<u32> = Some(10);
        let j: JsonString = o.into();
        assert_eq!(j, JsonString::from_json("10"));

        let o: Option<String> = Some("test".to_string());
        let j: JsonString = o.into();
        assert_eq!(j, JsonString::from_json("\"test\""));
    }

    #[test]
    fn json_string_to_option() {
        let j = JsonString::from("10");
        let o: JsonStringOption<u32> = j.try_into().expect("failed conversion from JsonString");
        assert_eq!(o.to_option(), Some(10));

        let j = JsonString::from("null");
        let o: JsonStringOption<u32> = j.try_into().expect("failed conversion from JsonString");
        assert_eq!(o.to_option(), None);

        // tricky!
        let j = JsonString::from("\"null\"");
        let o: JsonStringOption<String> = j.try_into().expect("failed conversion from JsonString");
        assert_eq!(o.to_option(), Some("null".to_string()));
    }
}