Skip to main content

neo_devpack/
utils.rs

1// Copyright (c) 2025-2026 R3E Network
2// Licensed under the MIT License
3
4//! General utility functions for Neo N3 smart contracts.
5//!
6//! For JSON serialization helpers, prefer the canonical functions in
7//! [`crate::storage`] (`read_json`, `write_json`, `struct_entry`, `value_to_json`).
8//! The functions here are thin wrappers kept for backward compatibility.
9
10use neo_types::{NeoByteString, NeoValue};
11use serde::{Deserialize, Serialize};
12use serde_json::Value as JsonValue;
13
14/// Deserializes a NeoByteString as JSON.
15///
16/// This is equivalent to [`crate::storage::read_json`].
17pub fn bytes_to_json<T: for<'de> Deserialize<'de>>(bytes: &NeoByteString) -> Option<T> {
18    crate::storage::read_json(bytes)
19}
20
21/// Serializes a value to JSON and returns it as a NeoByteString.
22///
23/// This is equivalent to [`crate::storage::write_json`].
24pub fn json_to_bytes<T: Serialize>(value: &T) -> neo_types::NeoResult<NeoByteString> {
25    crate::storage::write_json(value)
26}
27
28/// Creates a storage entry struct with key and value fields.
29///
30/// This is equivalent to [`crate::storage::struct_entry`].
31pub fn storage_struct(key: &NeoByteString, value: &NeoByteString) -> NeoValue {
32    crate::storage::struct_entry(key.clone(), value.clone())
33}
34
35/// Extracts JSON from a NeoValue containing a ByteString.
36///
37/// This is equivalent to [`crate::storage::value_to_json`].
38pub fn json_from_value(value: &NeoValue) -> Option<JsonValue> {
39    crate::storage::value_to_json(value)
40}