2021-02-13 07:31:18 -05:00
|
|
|
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
|
2019-12-20 08:47:20 -05:00
|
|
|
//! A JSON Parser and Stringifier.
|
2019-12-09 17:11:31 -05:00
|
|
|
use crate::Context;
|
2020-06-03 01:38:34 -04:00
|
|
|
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(
|
2020-04-13 08:43:56 -04:00
|
|
|
context: *const Context,
|
|
|
|
json_string: *const String,
|
|
|
|
) -> *const Value;
|
2019-12-09 17:11:31 -05:00
|
|
|
fn v8__JSON__Stringify(
|
2020-04-13 08:43:56 -04:00
|
|
|
context: *const Context,
|
|
|
|
json_object: *const Value,
|
|
|
|
) -> *const String;
|
2019-12-09 17:11:31 -05:00
|
|
|
}
|
|
|
|
|
2019-12-20 08:47:20 -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)]
|
2020-06-03 01:38:34 -04:00
|
|
|
pub fn parse<'s>(
|
|
|
|
scope: &mut HandleScope<'s>,
|
2020-05-31 12:45:00 -04:00
|
|
|
json_string: Local<'_, String>,
|
2020-06-03 01:38:34 -04:00
|
|
|
) -> Option<Local<'s, Value>> {
|
2020-06-25 20:56:24 -04:00
|
|
|
unsafe {
|
|
|
|
scope
|
|
|
|
.cast_local(|sd| v8__JSON__Parse(sd.get_current_context(), &*json_string))
|
|
|
|
}
|
2019-12-20 08:47:20 -05:00
|
|
|
}
|
2019-12-09 17:11:31 -05:00
|
|
|
|
2019-12-20 08:47:20 -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)]
|
2020-06-03 01:38:34 -04:00
|
|
|
pub fn stringify<'s>(
|
|
|
|
scope: &mut HandleScope<'s>,
|
2021-09-13 17:34:17 -04:00
|
|
|
json_object: Local<'_, Value>,
|
2020-06-03 01:38:34 -04:00
|
|
|
) -> Option<Local<'s, String>> {
|
2020-06-25 20:56:24 -04:00
|
|
|
unsafe {
|
|
|
|
scope.cast_local(|sd| {
|
|
|
|
v8__JSON__Stringify(sd.get_current_context(), &*json_object)
|
|
|
|
})
|
|
|
|
}
|
2019-12-09 17:11:31 -05:00
|
|
|
}
|