MCPcopy Create free account
hub / github.com/PowerShell/DSC / json_to_hashmap

Function json_to_hashmap

lib/dsc-lib/src/dscresources/command_resource.rs:1129–1172  ·  view source on GitHub ↗
(json: &str)

Source from the content-addressed store, hash-verified

1127}
1128
1129fn json_to_hashmap(json: &str) -> Result<HashMap<String, String>, DscError> {
1130 let mut map = HashMap::new();
1131 let json: Value = serde_json::from_str(json)?;
1132 if let Value::Object(obj) = json {
1133 for (key, value) in obj {
1134 match value {
1135 Value::String(s) => {
1136 map.insert(key, s);
1137 },
1138 Value::Bool(b) => {
1139 map.insert(key, b.to_string());
1140 },
1141 Value::Number(n) => {
1142 map.insert(key, n.to_string());
1143 },
1144 Value::Array(a) => {
1145 // only array of number or strings is supported
1146 let mut array = Vec::new();
1147 for v in a {
1148 match v {
1149 Value::String(s) => {
1150 array.push(s);
1151 },
1152 Value::Number(n) => {
1153 array.push(n.to_string());
1154 },
1155 _ => {
1156 return Err(DscError::Operation(t!("dscresources.commandResource.invalidArrayKey", key = key).to_string()));
1157 },
1158 }
1159 }
1160 map.insert(key, array.join(","));
1161 },
1162 Value::Null => {
1163 // ignore null values
1164 },
1165 Value::Object(_) => {
1166 return Err(DscError::Operation(t!("dscresources.commandResource.invalidKey", key = key).to_string()));
1167 },
1168 }
1169 }
1170 }
1171 Ok(map)
1172}
1173
1174/// Log output from a process as traces.
1175///

Callers 2

invoke_setFunction · 0.85
get_command_inputFunction · 0.85

Calls 3

newFunction · 0.50
from_strFunction · 0.50
OperationEnum · 0.50

Tested by

no test coverage detected