0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-27 01:29:19 -05:00
denoland-rusty-v8/src/json.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2021-02-13 07:31:18 -05:00
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
//! A JSON Parser and Stringifier.
2019-12-09 17:11:31 -05:00
use crate::Context;
use crate::HandleScope;
2019-12-09 17:11:31 -05:00
use crate::Local;
use crate::String;
use crate::Value;
extern "C" {
fn v8__JSON__Parse(
context: *const Context,
json_string: *const String,
) -> *const Value;
2019-12-09 17:11:31 -05:00
fn v8__JSON__Stringify(
context: *const Context,
json_object: *const Value,
) -> *const String;
2019-12-09 17:11:31 -05:00
}
/// Tries to parse the string `json_string` and returns it as value if
/// successful.
2022-09-20 22:45:33 -04:00
#[inline(always)]
pub fn parse<'s>(
scope: &mut HandleScope<'s>,
json_string: Local<'_, String>,
) -> Option<Local<'s, Value>> {
unsafe {
scope
.cast_local(|sd| v8__JSON__Parse(sd.get_current_context(), &*json_string))
}
}
2019-12-09 17:11:31 -05:00
/// Tries to stringify the JSON-serializable object `json_object` and returns
/// it as string if successful.
2022-09-20 22:45:33 -04:00
#[inline(always)]
pub fn stringify<'s>(
scope: &mut HandleScope<'s>,
json_object: Local<'_, Value>,
) -> Option<Local<'s, String>> {
unsafe {
scope.cast_local(|sd| {
v8__JSON__Stringify(sd.get_current_context(), &*json_object)
})
}
2019-12-09 17:11:31 -05:00
}