@@ -302,57 +302,42 @@ export namespace JSON {
302302 out . set < T > ( value ) ;
303303 return out ;
304304 }
305+ @inline private setPrimitive < T > ( value : T ) : void {
306+ if ( isBoolean ( value ) ) {
307+ this . type = JSON . Types . BoolNull ;
308+ store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
309+ } else if ( value instanceof u8 || value instanceof i8 ) {
310+ this . type = JSON . Types . U8Null ;
311+ store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
312+ } else if ( value instanceof u16 || value instanceof i16 ) {
313+ this . type = JSON . Types . U16 ;
314+ store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
315+ } else if ( value instanceof u32 || value instanceof i32 ) {
316+ this . type = JSON . Types . U32Null ;
317+ store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
318+ } else if ( value instanceof u64 || value instanceof i64 ) {
319+ this . type = JSON . Types . U64Null ;
320+ store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
321+ } else if ( value instanceof f32 ) {
322+ this . type = JSON . Types . F32Null ;
323+ store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
324+ } else if ( value instanceof f64 ) {
325+ this . type = JSON . Types . F64Null ;
326+ store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
327+ }
328+ }
305329 /**
306330 * Sets the value of the JSON.Value instance.
307331 * @param value - The value to be set.
308332 */
309333 @inline set < T > ( value : T ) : void {
310334 if ( isNullable < T > ( ) ) {
311-
335+ this . isNull = changetype < usize > ( value ) === 0 ;
312336 if ( value instanceof JSON . Box ) {
313- if ( isBoolean < T > ( ) ) {
314- this . type = JSON . Types . BoolNull ;
315- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
316- } else if ( value instanceof u8 || value instanceof i8 ) {
317- this . type = JSON . Types . U8Null ;
318- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
319- } else if ( value instanceof u16 || value instanceof i16 ) {
320- this . type = JSON . Types . U16Null ;
321- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
322- } else if ( value instanceof u32 || value instanceof i32 ) {
323- this . type = JSON . Types . U32Null ;
324- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
325- } else if ( value instanceof u64 || value instanceof i64 ) {
326- this . type = JSON . Types . U64Null ;
327- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
328- } else if ( value instanceof f32 ) {
329- this . type = JSON . Types . F32Null ;
330- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
331- } else if ( value instanceof f64 ) {
332- this . type = JSON . Types . F64Null ;
333- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
334- }
337+ return this . setPrimitive ( value . value ) ;
335338 } else if ( isBoolean < T > ( ) ) {
336339 this . type = JSON . Types . BoolNull ;
337340 store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
338- } else if ( value instanceof u8 || value instanceof i8 ) {
339- this . type = JSON . Types . U8Null ;
340- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
341- } else if ( value instanceof u16 || value instanceof i16 ) {
342- this . type = JSON . Types . U16Null ;
343- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
344- } else if ( value instanceof u32 || value instanceof i32 ) {
345- this . type = JSON . Types . U32Null ;
346- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
347- } else if ( value instanceof u64 || value instanceof i64 ) {
348- this . type = JSON . Types . U64Null ;
349- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
350- } else if ( value instanceof f32 ) {
351- this . type = JSON . Types . F32Null ;
352- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
353- } else if ( value instanceof f64 ) {
354- this . type = JSON . Types . F64Null ;
355- store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
356341 } else if ( isString < T > ( ) ) {
357342 this . type = JSON . Types . StringNull ;
358343 store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
@@ -381,7 +366,6 @@ export namespace JSON {
381366 this . type = JSON . Types . ArrayNull ;
382367 store < T > ( changetype < usize > ( this ) , value , STORAGE ) ;
383368 }
384- this . isNull = changetype < usize > ( value ) === 0 ;
385369 } else {
386370 if ( value instanceof JSON . Box ) {
387371 return this . set ( value . value ) ;
@@ -457,6 +441,16 @@ export namespace JSON {
457441 return load < T > ( changetype < usize > ( this ) , STORAGE ) ;
458442 }
459443
444+ /**
445+ * Gets the value of the JSON.Value instance as a Box<T>.
446+ * Alias for .get<T>()
447+ * @returns The encapsulated value.
448+ */
449+ @inline asBox < T > ( ) : Box < T > | null {
450+ if ( this . isNull ) return null ;
451+ return changetype < Box < T > > ( JSON . Box . fromValue < T > ( this ) ) ;
452+ }
453+
460454 /**
461455 * Converts the JSON.Value to a string representation.
462456 * @returns The string representation of the JSON.Value.
@@ -598,6 +592,22 @@ export namespace JSON {
598592 this . value = value ;
599593 return this ;
600594 }
595+ /**
596+ * Creates a Box<T> | null from a JSON.Value
597+ * This means that it can create a nullable primitive from a JSON.Value
598+ * ```js
599+ * const value = JSON.parse<i32>("null"); // -> Box<i32> | null
600+ * const boxed = JSON.Box.fromValue<i32>(value); // -> Box<i32> | null
601+ * // null
602+ * ```
603+ * @param from T
604+ * @returns Box<T> | null
605+ */
606+ @inline static fromValue < T > ( value : JSON . Value ) : Box < T > | null {
607+ if ( ! ( value instanceof JSON . Value ) ) throw new Error ( "value must be of type JSON.Value" ) ;
608+ if ( value . isNull ) return null ;
609+ return new Box ( value . get < T > ( ) ) ;
610+ }
601611 /**
602612 * Creates a reference to a primitive type
603613 * This means that it can create a nullable primitive
0 commit comments