iota_graphql_rpc/types/
json.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::fmt;
6
7use async_graphql::*;
8
9/// Arbitrary JSON data.
10#[derive(Debug)]
11pub(crate) struct Json(Value);
12
13#[Scalar(name = "JSON", use_type_description = true)]
14impl ScalarType for Json {
15    fn parse(value: Value) -> InputValueResult<Self> {
16        Ok(Self(value))
17    }
18
19    fn to_value(&self) -> Value {
20        self.0.clone()
21    }
22}
23
24impl Description for Json {
25    fn description() -> &'static str {
26        "Arbitrary JSON data."
27    }
28}
29
30impl From<Value> for Json {
31    fn from(value: Value) -> Self {
32        Self(value)
33    }
34}
35
36impl fmt::Display for Json {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        self.0.fmt(f)
39    }
40}