feat: impl FromNapiValue
for serde_json::Number
, fix it for Null
, throw for impossible types (#1052)
fix #1013 Co-authored-by: zeroslope <jsx55242@foxmail.com>
This commit is contained in:
parent
c001038852
commit
ed12bd76bd
6 changed files with 117 additions and 22 deletions
examples/napi
|
@ -123,6 +123,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
}␊
|
||||
export function readPackageJson(): PackageJson␊
|
||||
export function getPackageJsonName(packageJson: PackageJson): string␊
|
||||
export function testSerdeRoundtrip(data: any): any␊
|
||||
export function contains(source: string, target: string): boolean␊
|
||||
export function concatStr(s: string): string␊
|
||||
export function concatUtf16(s: string): string␊
|
||||
|
|
Binary file not shown.
|
@ -80,6 +80,7 @@ import {
|
|||
receiveMutClassOrNumber,
|
||||
getStrFromObject,
|
||||
returnJsFunction,
|
||||
testSerdeRoundtrip,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -299,6 +300,34 @@ test('serde-json', (t) => {
|
|||
t.is(getPackageJsonName(packageJson), 'napi-rs')
|
||||
})
|
||||
|
||||
test('serde-roundtrip', (t) => {
|
||||
t.is(testSerdeRoundtrip(1), 1)
|
||||
t.is(testSerdeRoundtrip(1.2), 1.2)
|
||||
t.is(testSerdeRoundtrip(-1), -1)
|
||||
|
||||
t.deepEqual(testSerdeRoundtrip([1, 1.2, -1]), [1, 1.2, -1])
|
||||
t.deepEqual(testSerdeRoundtrip({ a: 1, b: 1.2, c: -1 }), {
|
||||
a: 1,
|
||||
b: 1.2,
|
||||
c: -1,
|
||||
})
|
||||
t.throws(() => testSerdeRoundtrip(NaN))
|
||||
|
||||
t.is(testSerdeRoundtrip(null), null)
|
||||
|
||||
let err = t.throws(() => testSerdeRoundtrip(undefined))
|
||||
t.is(err!.message, 'undefined cannot be represented as a serde_json::Value')
|
||||
|
||||
err = t.throws(() => testSerdeRoundtrip(() => {}))
|
||||
t.is(
|
||||
err!.message,
|
||||
'JS functions cannot be represented as a serde_json::Value',
|
||||
)
|
||||
|
||||
err = t.throws(() => testSerdeRoundtrip(Symbol.for('foo')))
|
||||
t.is(err!.message, 'JS symbols cannot be represented as a serde_json::Value')
|
||||
})
|
||||
|
||||
test('buffer', (t) => {
|
||||
let buf = getBuffer()
|
||||
t.is(buf.toString('utf-8'), 'Hello world')
|
||||
|
|
1
examples/napi/index.d.ts
vendored
1
examples/napi/index.d.ts
vendored
|
@ -113,6 +113,7 @@ export interface PackageJson {
|
|||
}
|
||||
export function readPackageJson(): PackageJson
|
||||
export function getPackageJsonName(packageJson: PackageJson): string
|
||||
export function testSerdeRoundtrip(data: any): any
|
||||
export function contains(source: string, target: string): boolean
|
||||
export function concatStr(s: string): string
|
||||
export function concatUtf16(s: string): string
|
||||
|
|
|
@ -25,3 +25,8 @@ fn read_package_json() -> Result<PackageJson> {
|
|||
fn get_package_json_name(package_json: PackageJson) -> String {
|
||||
package_json.name
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn test_serde_roundtrip(data: Value) -> Value {
|
||||
data
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue