@@ -107,51 +107,40 @@ impl IntoJsResult for DateTime {
107
107
}
108
108
}
109
109
110
- // impl IntoJsResult for JsonHashMap {
111
- // type Output = JsObject;
112
- // fn into_js_result<'a, 'b, 'c: 'b, C: Context<'c>>(
113
- // self,
114
- // cx: &mut C,
115
- // ) -> JsResult<'b, Self::Output> {
116
- // self.0 .0.into_js_result(cx)
117
- // }
118
- // }
119
-
120
110
impl IntoJsResult for Json {
121
- type Output = JsObject ;
111
+ type Output = JsValue ;
122
112
fn into_js_result < ' a , ' b , ' c : ' b , C : Context < ' c > > (
123
113
self ,
124
114
cx : & mut C ,
125
115
) -> JsResult < ' b , Self :: Output > {
126
- let js_object = JsObject :: new ( cx) ;
127
- for ( k, v) in self
128
- . 0
129
- . as_object ( )
130
- . expect ( "We currently only support json objects" )
131
- . iter ( )
132
- {
133
- let js_key = cx. string ( k) ;
134
- match v {
135
- // TODO: Support more types like nested objects
136
- serde_json:: Value :: Number ( x) => {
137
- let js_value = x
138
- . as_f64 ( )
139
- . expect ( "Error converting to f64 in impl IntoJsResult for Json" ) ;
140
- let js_value = JsNumber :: new ( cx, js_value) ;
141
- js_object. set ( cx, js_key, js_value) ?;
116
+ match self . 0 {
117
+ serde_json:: Value :: Bool ( x) => Ok ( JsBoolean :: new ( cx, x) . upcast ( ) ) ,
118
+ serde_json:: Value :: Number ( x) => Ok ( JsNumber :: new (
119
+ cx,
120
+ x. as_f64 ( )
121
+ . expect ( "Error converting to f64 in impl IntoJsResult for Json" ) ,
122
+ )
123
+ . upcast ( ) ) ,
124
+ serde_json:: Value :: String ( x) => Ok ( JsString :: new ( cx, & x) . upcast ( ) ) ,
125
+ serde_json:: Value :: Array ( x) => {
126
+ let js_array = JsArray :: new ( cx, x. len ( ) as u32 ) ;
127
+ for ( i, v) in x. into_iter ( ) . enumerate ( ) {
128
+ let js_value = Json :: into_js_result ( Self ( v) , cx) ?;
129
+ js_array. set ( cx, i as u32 , js_value) ?;
142
130
}
143
- serde_json:: Value :: Bool ( x) => {
144
- let js_value = JsBoolean :: new ( cx, * x) ;
145
- js_object. set ( cx, js_key, js_value) ?;
146
- }
147
- serde_json:: Value :: String ( x) => {
148
- let js_value = cx. string ( x) ;
131
+ Ok ( js_array. upcast ( ) )
132
+ }
133
+ serde_json:: Value :: Object ( x) => {
134
+ let js_object = JsObject :: new ( cx) ;
135
+ for ( k, v) in x. into_iter ( ) {
136
+ let js_key = cx. string ( k) ;
137
+ let js_value = Json :: into_js_result ( Self ( v) , cx) ?;
149
138
js_object. set ( cx, js_key, js_value) ?;
150
139
}
151
- _ => { }
140
+ Ok ( js_object . upcast ( ) )
152
141
}
142
+ _ => panic ! ( "Unsupported type for JSON conversion" ) ,
153
143
}
154
- Ok ( js_object)
155
144
}
156
145
}
157
146
@@ -303,36 +292,47 @@ impl<K: FromJsType + std::hash::Hash + std::fmt::Display + std::cmp::Eq, V: From
303
292
}
304
293
305
294
impl FromJsType for Json {
306
- type From = JsObject ;
295
+ type From = JsValue ;
307
296
fn from_js_type < ' a , C : Context < ' a > > ( cx : & mut C , arg : Handle < Self :: From > ) -> NeonResult < Self > {
308
- let mut json = serde_json:: Map :: new ( ) ;
309
- let keys = arg. get_own_property_names ( cx) ?. to_vec ( cx) ?;
310
- for key in keys {
311
- let key: Handle < JsString > = key. downcast ( cx) . or_throw ( cx) ?;
312
- let key: String = String :: from_js_type ( cx, key) ?;
313
- let value: Handle < JsValue > = arg. get ( cx, key. as_str ( ) ) ?;
314
- // TODO: Support for more types
315
- if value. is_a :: < JsString , _ > ( cx) {
316
- let value: Handle < JsString > = value. downcast ( cx) . or_throw ( cx) ?;
317
- let value: String = String :: from_js_type ( cx, value) ?;
318
- let value = serde_json:: Value :: String ( value) ;
319
- json. insert ( key, value) ;
320
- } else if value. is_a :: < JsNumber , _ > ( cx) {
321
- let value: Handle < JsNumber > = value. downcast ( cx) . or_throw ( cx) ?;
322
- let value: f64 = f64:: from_js_type ( cx, value) ?;
323
- let value = serde_json:: value:: Number :: from_f64 ( value)
324
- . expect ( "Could not convert f64 to serde_json::Number" ) ;
325
- let value = serde_json:: value:: Value :: Number ( value) ;
326
- json. insert ( key, value) ;
327
- } else if value. is_a :: < JsBoolean , _ > ( cx) {
328
- let value: Handle < JsBoolean > = value. downcast ( cx) . or_throw ( cx) ?;
329
- let value: bool = bool:: from_js_type ( cx, value) ?;
330
- let value = serde_json:: Value :: Bool ( value) ;
331
- json. insert ( key, value) ;
332
- } else {
333
- panic ! ( "Unsupported type for json conversion" ) ;
297
+ if arg. is_a :: < JsArray , _ > ( cx) {
298
+ let value: Handle < JsArray > = arg. downcast ( cx) . or_throw ( cx) ?;
299
+ let mut json = Vec :: new ( ) ;
300
+ for item in value. to_vec ( cx) ? {
301
+ let item = Json :: from_js_type ( cx, item) ?;
302
+ json. push ( item. 0 ) ;
303
+ }
304
+ Ok ( Self ( serde_json:: Value :: Array ( json) ) )
305
+ } else if arg. is_a :: < JsBoolean , _ > ( cx) {
306
+ let value: Handle < JsBoolean > = arg. downcast ( cx) . or_throw ( cx) ?;
307
+ let value = bool:: from_js_type ( cx, value) ?;
308
+ let value = serde_json:: Value :: Bool ( value) ;
309
+ Ok ( Self ( value) )
310
+ } else if arg. is_a :: < JsString , _ > ( cx) {
311
+ let value: Handle < JsString > = arg. downcast ( cx) . or_throw ( cx) ?;
312
+ let value = String :: from_js_type ( cx, value) ?;
313
+ let value = serde_json:: Value :: String ( value) ;
314
+ Ok ( Self ( value) )
315
+ } else if arg. is_a :: < JsNumber , _ > ( cx) {
316
+ let value: Handle < JsNumber > = arg. downcast ( cx) . or_throw ( cx) ?;
317
+ let value = f64:: from_js_type ( cx, value) ?;
318
+ let value = serde_json:: value:: Number :: from_f64 ( value)
319
+ . expect ( "Could not convert f64 to serde_json::Number" ) ;
320
+ let value = serde_json:: value:: Value :: Number ( value) ;
321
+ Ok ( Self ( value) )
322
+ } else if arg. is_a :: < JsObject , _ > ( cx) {
323
+ let value: Handle < JsObject > = arg. downcast ( cx) . or_throw ( cx) ?;
324
+ let mut json = serde_json:: Map :: new ( ) ;
325
+ let keys = value. get_own_property_names ( cx) ?. to_vec ( cx) ?;
326
+ for key in keys {
327
+ let key: Handle < JsString > = key. downcast ( cx) . or_throw ( cx) ?;
328
+ let key: String = String :: from_js_type ( cx, key) ?;
329
+ let json_value: Handle < JsValue > = value. get ( cx, key. as_str ( ) ) ?;
330
+ let json_value = Json :: from_js_type ( cx, json_value) ?;
331
+ json. insert ( key, json_value. 0 ) ;
334
332
}
333
+ Ok ( Self ( serde_json:: Value :: Object ( json) ) )
334
+ } else {
335
+ panic ! ( "Unsupported type for Json conversion" ) ;
335
336
}
336
- Ok ( Self ( serde_json:: Value :: Object ( json) ) )
337
337
}
338
338
}
0 commit comments