0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-03-10 05:56:52 -04:00
rusty-v8/src/json.rs

36 lines
1,006 B
Rust
Raw Normal View History

2020-01-02 13:57:00 -05:00
// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license.
//! A JSON Parser and Stringifier.
2019-12-09 23:11:31 +01:00
use crate::Context;
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 23:11:31 +01:00
fn v8__JSON__Stringify(
context: *const Context,
json_object: *const Value,
) -> *const String;
2019-12-09 23:11:31 +01:00
}
/// Tries to parse the string `json_string` and returns it as value if
/// successful.
2019-12-20 16:01:45 +01:00
pub fn parse<'sc>(
context: Local<'sc, Context>,
json_string: Local<'sc, String>,
2019-12-20 16:01:45 +01:00
) -> Option<Local<'sc, Value>> {
unsafe { Local::from_raw(v8__JSON__Parse(&*context, &*json_string)) }
}
2019-12-09 23:11:31 +01:00
/// Tries to stringify the JSON-serializable object `json_object` and returns
/// it as string if successful.
2019-12-20 16:01:45 +01:00
pub fn stringify<'sc>(
context: Local<'sc, Context>,
json_object: Local<'sc, Value>,
2019-12-20 16:01:45 +01:00
) -> Option<Local<'sc, String>> {
unsafe { Local::from_raw(v8__JSON__Stringify(&*context, &*json_object)) }
2019-12-09 23:11:31 +01:00
}