Skip to content

Commit

Permalink
Add JSON_GetType and JSON_Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyB123 committed Oct 9, 2022
1 parent f779403 commit a73c613
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
6 changes: 6 additions & 0 deletions json.inc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ native JSON_GetFloat(Node:node, const key[], &Float:output);
native JSON_GetBool(Node:node, const key[], &bool:output);
native JSON_GetString(Node:node, const key[], output[], len = sizeof(output));

// JSON_GetType returns the type of a node's key
native JSON_NODE:JSON_GetType(Node:node, const key[]);

// JSON_GetArray returns the `Node:` stored at `index` in the given `node`. The
// `Node:` returned could be an Object or a primitive, such as an int, float,
// bool or string. Use functions below to convert `Node:` into a native type.
Expand Down Expand Up @@ -159,6 +162,9 @@ native JSON_ArrayRemoveIndex(Node:node, const key[], index);
// JSON_ArrayClear empties out all of the items in a JSON array.
native JSON_ArrayClear(Node:node, const key[]);

// JSON_Keys can be used to iterate through the keys inside of a JSON node
native JSON_Keys(Node:node, index, output[], len = sizeof(output));

// JSON_GetNode* functions extract a JSON object `Node:` to `output`.
// These are useful for when you get a `Node:` that represents a primitive type
// such as from JSON_GetArray.
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ initialize_plugin!(
Plugin::json_get_bool,
Plugin::json_get_string,
Plugin::json_get_array,
Plugin::json_get_type,
Plugin::json_array_length,
Plugin::json_array_object,
Plugin::json_array_append,
Plugin::json_array_remove,
Plugin::json_array_remove_index,
Plugin::json_array_clear,
Plugin::json_keys,
Plugin::json_remove,
Plugin::json_get_node_int,
Plugin::json_get_node_float,
Expand Down
52 changes: 51 additions & 1 deletion src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl Plugin {

dst[key.to_string()] = src;
Ok(0)
}
}

#[native(name = "JSON_SetInt")]
pub fn json_set_int(
Expand Down Expand Up @@ -597,6 +597,31 @@ impl Plugin {
Ok(0)
}

#[native(name = "JSON_GetType")]
pub fn json_get_type(&mut self, _: &Amx, node: i32, key: AmxString) -> AmxResult<i32> {
let v: &mut serde_json::Value = match self.json_nodes.get(node) {
Some(v) => v,
None => return Ok(0),
};
if !v.is_object() {
return Ok(0);
}

if v.get(key.to_string()) == None {
return Ok(0);
}

let t: i32 = match v[key.to_string()] {
serde_json::Value::Null => JsonNode::Null as i32,
serde_json::Value::Bool(_) => JsonNode::Boolean as i32,
serde_json::Value::Number(_) => JsonNode::Number as i32,
serde_json::Value::String(_) => JsonNode::String as i32,
serde_json::Value::Array(_) => JsonNode::Array as i32,
serde_json::Value::Object(_) => JsonNode::Object as i32,
};
Ok(t)
}

#[native(name = "JSON_ArrayLength")]
pub fn json_array_length(
&mut self,
Expand Down Expand Up @@ -728,6 +753,31 @@ impl Plugin {
Ok(0)
}

#[native(name = "JSON_Keys")]
pub fn json_keys(&mut self, _: &Amx, node: i32, index: i32, value: UnsizedBuffer, length: usize) -> AmxResult<i32> {
let v: &mut serde_json::Value = match self.json_nodes.get(node) {
Some(v) => v,
None => return Ok(1),
};

if !v.is_object() {
return Ok(1);
}

let mut vec: Vec<String> = vec![];
for key in v.as_object().unwrap().keys() {
vec.push(key.to_string());
}

if index as usize >= vec.len() {
return Ok(1)
}

let mut dest = value.into_sized_buffer(length);
let _ = samp::cell::string::put_in_buffer(&mut dest, &vec[index as usize]);
Ok(0)
}

#[native(name = "JSON_Remove")]
pub fn json_remove(&mut self, _: &Amx, node: i32, key: AmxString) -> AmxResult<i32> {
let v: &mut serde_json::Value = match self.json_nodes.get(node) {
Expand Down

0 comments on commit a73c613

Please sign in to comment.