@@ -2,35 +2,50 @@ import { Component, Prop, Part } from '@pictogrammers/element';
22
33import PgJsonArray from '../jsonArray/jsonArray' ;
44import PgJsonObject from '../jsonObject/jsonObject' ;
5- import PgJsonString from '../jsonString/jsonString' ;
65
76import template from './json.html' ;
87import style from './json.css' ;
98
109function unwrapObject ( obj : any ) {
11- return {
12- type : PgJsonObject ,
13- items : Object . keys ( obj ) . map ( ( key ) => {
10+ return Object . keys ( obj ) . map ( ( key ) => {
11+ if ( Array . isArray ( obj [ key ] ) ) {
1412 return {
1513 key,
16- value : obj [ key ] ,
17- type : PgJsonString ,
18- }
19- } )
20- } ;
14+ value : unwrapArray ( obj [ key ] ) ,
15+ } ;
16+ }
17+ if ( typeof obj [ key ] === 'object' ) {
18+ return {
19+ key,
20+ value : unwrapObject ( obj [ key ] ) ,
21+ } ;
22+ }
23+ return {
24+ key,
25+ value : obj [ key ] ,
26+ } ;
27+ } ) ;
2128}
2229
2330function unwrapArray ( items : any ) {
24- return {
25- type : PgJsonArray ,
26- items : items . map ( ( item ) => {
27- if ( Array . isArray ( item ) ) {
28- return unwrapArray ( item ) ;
29- } else {
30- return unwrapObject ( item ) ;
31- }
32- } )
33- } ;
31+ return items . map ( ( item , i ) => {
32+ if ( Array . isArray ( item ) ) {
33+ return {
34+ key : i . toString ( ) ,
35+ value : unwrapArray ( item ) ,
36+ } ;
37+ }
38+ if ( typeof item === 'object' ) {
39+ return {
40+ key : i . toString ( ) ,
41+ value : unwrapObject ( item ) ,
42+ } ;
43+ }
44+ return {
45+ key : i . toString ( ) ,
46+ value : item ,
47+ }
48+ } ) ;
3449}
3550
3651@Component ( {
@@ -49,11 +64,11 @@ export default class PgJson extends HTMLElement {
4964 if ( changes . value && this . value !== null ) {
5065 if ( typeof this . value === 'object' ) {
5166 const $object = document . createElement ( 'pg-json-object' ) as PgJsonObject ;
52- $object . items = unwrapObject ( this . value ) . items ;
67+ $object . value = unwrapObject ( this . value ) ;
5368 this . $container . appendChild ( $object ) ;
5469 } else if ( Array . isArray ( this . value ) ) {
5570 const $array = document . createElement ( 'pg-json-array' ) as PgJsonArray ;
56- $array . items = unwrapArray ( this . value ) . items ;
71+ $array . value = unwrapArray ( this . value ) ;
5772 this . $container . appendChild ( $array ) ;
5873 }
5974 }
0 commit comments