MCPcopy Create free account
hub / github.com/dprint/jsonc-parser / from

Method from

src/ast.rs:62–106  ·  view source on GitHub ↗
(value: Value<'a>)

Source from the content-addressed store, hash-verified

60#[cfg(feature = "serde_json")]
61impl<'a> From<Value<'a>> for serde_json::Value {
62 fn from(value: Value<'a>) -> Self {
63 use std::str::FromStr;
64 match value {
65 Value::Array(arr) => {
66 let vec = arr.elements.into_iter().map(|v| v.into()).collect();
67 serde_json::Value::Array(vec)
68 }
69 Value::BooleanLit(b) => serde_json::Value::Bool(b.value),
70 Value::NullKeyword(_) => serde_json::Value::Null,
71 Value::NumberLit(num) => {
72 // check if this is a hexadecimal literal (0x or 0X prefix)
73 let num_str = num.value.trim_start_matches(['-', '+']);
74 if num_str.len() > 2 && (num_str.starts_with("0x") || num_str.starts_with("0X")) {
75 // Parse hexadecimal and convert to decimal
76 let hex_part = &num_str[2..];
77 match i64::from_str_radix(hex_part, 16) {
78 Ok(decimal_value) => {
79 let final_value = if num.value.starts_with('-') {
80 -decimal_value
81 } else {
82 decimal_value
83 };
84 serde_json::Value::Number(serde_json::Number::from(final_value))
85 }
86 Err(_) => serde_json::Value::String(num.value.to_string()),
87 }
88 } else {
89 // standard decimal number
90 let num_for_parsing = num.value.trim_start_matches('+');
91 match serde_json::Number::from_str(num_for_parsing) {
92 Ok(number) => serde_json::Value::Number(number),
93 Err(_) => serde_json::Value::String(num.value.to_string()),
94 }
95 }
96 }
97 Value::Object(obj) => {
98 let mut map = serde_json::map::Map::new();
99 for prop in obj.properties {
100 map.insert(prop.name.into_string(), prop.value.into());
101 }
102 serde_json::Value::Object(map)
103 }
104 Value::StringLit(s) => serde_json::Value::String(s.value.into_owned()),
105 }
106 }
107}
108
109/// Node that can appear in the AST.

Callers

nothing calls this directly

Calls 10

ArrayClass · 0.85
ObjectClass · 0.85
BooleanLitClass · 0.85
NullKeywordClass · 0.85
NumberLitClass · 0.85
StringLitClass · 0.85
into_iterMethod · 0.80
lenMethod · 0.80
insertMethod · 0.80
into_stringMethod · 0.45

Tested by

no test coverage detected