olosegres / jsona

Tiny and simple JSON:API serializer / deserializer. Creates simplified objects from JSON or stored reduxObject, creates JSON from the same simplified objects (in according with JSON API specification)
MIT License
206 stars 27 forks source link

Nested json api object is not properly deserilized #61

Closed lexer closed 1 year ago

lexer commented 1 year ago

Example:

{
   "data":{
      "type":"event",
      "id":"evt_9pjxVWctAtwYo3EsjfeuBjXT",
      "attributes":{
         "name":"inquiry.transitioned",
         "payload":{
            "data":{
               "type":"inquiry",
               "id":"inq_ZWRL8Dr2KjhHcewoj3meoDU3",
               "attributes":{
                  "status":"created",
                  "reference-id":"clblepigj003ixrii2r1i03li_us",
                  "note":null,
                  "behaviors":{
                     "request-spoof-attempts":null,
                     "user-agent-spoof-attempts":null,
                     "distraction-events":null,
                     "hesitation-baseline":null,
                     "hesitation-count":null,
                     "hesitation-time":null,
                     "shortcut-copies":null,
                     "shortcut-pastes":null,
                     "autofill-cancels":null,
                     "autofill-starts":null,
                     "devtools-open":null,
                     "completion-time":null,
                     "hesitation-percentage":null,
                     "behavior-threat-level":null
                  },
                  "tags":[

                  ],
                  "creator":"bob@gmail.com",
                  "reviewer-comment":null,
                  "created-at":"2023-02-13T23:28:43.000Z",
                  "started-at":null,
                  "completed-at":null,
                  "failed-at":null,
                  "marked-for-review-at":null,
                  "decisioned-at":null,
                  "expired-at":null,
                  "redacted-at":null,
                  "previous-step-name":"ui_step",
                  "next-step-name":"ui_step_2",
                  "name-first":"John",
                  "name-middle":null,
                  "name-last":"Doe",
                  "birthdate":"1985-10-27",
                  "address-street-1":null,
                  "address-street-2":null,
                  "address-city":null,
                  "address-subdivision":null,
                  "address-subdivision-abbr":null,
                  "address-postal-code":null,
                  "address-postal-code-abbr":null,
                  "social-security-number":null,
                  "identification-number":null,
                  "email-address":null,
                  "phone-number":null,
                  "fields":{
                     "birthdate":{
                        "type":"date",
                        "value":"1985-10-22"
                     },
                     "name-last":{
                        "type":"string",
                        "value":"Doe"
                     },
                     "name-first":{
                        "type":"string",
                        "value":"John"
                     },
                     "name-middle":{
                        "type":"string",
                        "value":null
                     },
                     "address-city":{
                        "type":"string",
                        "value":null
                     },
                     "phone-number":{
                        "type":"string",
                        "value":null
                     },
                     "email-address":{
                        "type":"string",
                        "value":null
                     },
                     "current-selfie":{
                        "type":"selfie",
                        "value":null
                     },
                     "address-street-1":{
                        "type":"string",
                        "value":null
                     },
                     "address-street-2":{
                        "type":"string",
                        "value":null
                     },
                     "selected-id-class":{
                        "type":"string",
                        "value":null
                     },
                     "address-postal-code":{
                        "type":"string",
                        "value":null
                     },
                     "address-subdivision":{
                        "type":"string",
                        "value":null
                     },
                     "address-country-code":{
                        "type":"string",
                        "value":null
                     },
                     "identification-class":{
                        "type":"string",
                        "value":null
                     },
                     "current-government-id":{
                        "type":"government_id",
                        "value":null
                     },
                     "identification-number":{
                        "type":"string",
                        "value":null
                     },
                     "selected-country-code":{
                        "type":"string",
                        "value":"US"
                     },
                     "social-security-number":{
                        "type":"string",
                        "value":null
                     }
                  }
               },
               "relationships":{
                  "account":{
                     "data":{
                        "type":"account",
                        "id":"act_sT7d9rV6cpedbay3KqFE8XyQ"
                     }
                  },
                  "template":{
                     "data":null
                  },
                  "inquiry-template":{
                     "data":{
                        "type":"inquiry-template",
                        "id":"itmpl_Y4TP7U7gX9Uyd8ocVtTBhe5a"
                     }
                  },
                  "inquiry-template-version":{
                     "data":{
                        "type":"inquiry-template-version",
                        "id":"itmplv_LiqByoYHERRpZQ6YmcxanZe5"
                     }
                  },
                  "reviewer":{
                     "data":null
                  },
                  "reports":{
                     "data":[

                     ]
                  },
                  "verifications":{
                     "data":[

                     ]
                  },
                  "sessions":{
                     "data":[
                        {
                           "type":"inquiry-session",
                           "id":"iqse_WVV6Jyt3Xj8pfZ2dReAaPM7e"
                        },
                        {
                           "type":"inquiry-session",
                           "id":"iqse_XJKSyAFd9ZuEzNXbPTSR5697"
                        }
                     ]
                  },
                  "documents":{
                     "data":[

                     ]
                  },
                  "selfies":{
                     "data":[

                     ]
                  }
               }
            },
            "included":[
               {
                  "type":"account",
                  "id":"act_sT7d9rV6cpedbay3KqFE8XyQ",
                  "attributes":{
                     "reference-id":"clblepigj003ixrii2r1i03li_us",
                     "created-at":"2023-02-13T22:38:59.000Z",
                     "updated-at":"2023-02-13T23:06:57.000Z",
                     "redacted-at":null,
                     "name-first":"John",
                     "name-middle":null,
                     "name-last":"Doe",
                     "phone-number":null,
                     "email-address":null,
                     "address-street-1":"600 CALIFORNIA STREET",
                     "address-street-2":"609",
                     "address-city":"SAN FRANCISCO",
                     "address-subdivision":"CA",
                     "address-postal-code":"94109",
                     "country-code":"US",
                     "birthdate":"1985-10-21",
                     "social-security-number":null,
                     "tags":[

                     ],
                     "identification-numbers":{
                        "":[
                           {
                              "issuing-country":null,
                              "identification-class":null,
                              "identification-number":"680-47-4980",
                              "created-at":"2023-02-13T22:39:48.666Z",
                              "updated-at":"2023-02-13T22:39:48.666Z"
                           },
                           {
                              "issuing-country":null,
                              "identification-class":null,
                              "identification-number":"680-48-4970",
                              "created-at":"2023-02-13T22:44:15.979Z",
                              "updated-at":"2023-02-13T22:44:15.979Z"
                           }
                        ],
                        "dl":[
                           {
                              "issuing-country":"US",
                              "identification-class":"dl",
                              "identification-number":"I1234562",
                              "created-at":"2023-02-13T23:03:25.496Z",
                              "updated-at":"2023-02-13T23:03:25.496Z"
                           }
                        ]
                     }
                  }
               },
               {
                  "type":"inquiry-template",
                  "id":"itmpl_Y4TP7U7gX9Uyd8ocVtTBhe5a",
                  "attributes":{
                     "status":"active",
                     "name":"Government ID and Selfie"
                  }
               },
               {
                  "type":"inquiry-template-version",
                  "id":"itmplv_LiqByoYHERRpZQ6YmcxanZe5",
                  "attributes":{
                     "name-display":"",
                     "enabled-locales":[
                        "ar-EG",
                        "bn",
                        "de",
                        "en-US",
                        "es-MX",
                        "fr",
                        "he",
                        "hi",
                        "hu",
                        "id-ID",
                        "it",
                        "ja",
                        "ko-KR",
                        "lt",
                        "ms",
                        "nl-NL",
                        "pl",
                        "pt-BR",
                        "ro",
                        "ru",
                        "sv",
                        "ta",
                        "th",
                        "tr-TR",
                        "uk-UA",
                        "vi",
                        "zh-CN",
                        "zh-TW"
                     ],
                     "theme":{
                        "border-radius":null,
                        "border-radius-modal":null,
                        "border-width":null,
                        "button-background-image":null,
                        "button-font-weight":null,
                        "button-position":null,
                        "button-shadow-strength":null,
                        "button-text-transform":null,
                        "color-button-primary":null,
                        "color-button-secondary":null,
                        "color-button-secondary-fill":null,
                        "color-button-primary-fill-disabled":null,
                        "color-button-secondary-fill-disabled":null,
                        "color-error":null,
                        "color-font":null,
                        "color-font-button-primary":null,
                        "color-font-button-secondary":null,
                        "color-font-small":null,
                        "color-font-title":null,
                        "color-icon-header":null,
                        "color-input-background":null,
                        "color-input-border":null,
                        "color-link":null,
                        "color-modal-background":null,
                        "color-primary":null,
                        "color-progress-bar":null,
                        "color-success":null,
                        "color-warning":null,
                        "color-divider":null,
                        "color-dropdown-background":null,
                        "color-dropdown-option":null,
                        "font-family":null,
                        "font-family-title":null,
                        "font-url":null,
                        "font-size-body":null,
                        "font-size-header":null,
                        "font-size-small":null,
                        "line-height-body":null,
                        "line-height-header":null,
                        "line-height-small":null,
                        "header-font-weight":null,
                        "header-margin-bottom":null,
                        "icon-color-primary":null,
                        "icon-color-highlight":null,
                        "icon-color-stroke":null,
                        "icon-color-background":null,
                        "icon-color-government-id-type":null,
                        "icon-style":null,
                        "input-style":null,
                        "page-transition":null,
                        "text-align":null,
                        "text-decoration-line-link":null,
                        "us-state-input-method":null,
                        "vertical-options-style":null,
                        "government-id-pictograph-position":null,
                        "id-back-pictograph-height":null,
                        "id-back-pictograph-url":null,
                        "id-front-pictograph-height":null,
                        "id-front-pictograph-url":null,
                        "passport-front-pictograph-height":null,
                        "passport-front-pictograph-url":null,
                        "passport-signature-pictograph-height":null,
                        "passport-signature-pictograph-url":null,
                        "government-id-select-pictograph-height":null,
                        "government-id-select-pictograph-url":null,
                        "device-handoff-terms-text-position":null,
                        "selfie-pictograph-url":null,
                        "selfie-pictograph-height":null,
                        "selfie-terms-text-position":null,
                        "selfie-center-pictograph-url":null,
                        "selfie-center-pictograph-height":null,
                        "selfie-left-pictograph-url":null,
                        "selfie-left-pictograph-height":null,
                        "selfie-right-pictograph-url":null,
                        "selfie-right-pictograph-height":null,
                        "document-pictograph-position":null,
                        "document-pictograph-height":null,
                        "document-pictograph-url":null,
                        "camera-support-pictograph-height":null,
                        "camera-support-pictograph-url":null,
                        "loading-pictograph-height":null,
                        "loading-pictograph-url":null
                     }
                  }
               },
               {
                  "type":"inquiry-session",
                  "id":"iqse_WVV6Jyt3Xj8pfZ2dReAaPM7e",
                  "attributes":{
                     "status":"new",
                     "created-at":"2023-02-13T23:28:51.000Z",
                     "ip-address":null,
                     "user-agent":null,
                     "os-name":null,
                     "os-full-version":null,
                     "device-type":null,
                     "device-name":null,
                     "browser-name":null,
                     "browser-full-version":null,
                     "mobile-sdk-name":null,
                     "mobile-sdk-full-version":null,
                     "is-proxy":null,
                     "is-tor":null,
                     "is-datacenter":null,
                     "threat-level":null,
                     "country-code":null,
                     "country-name":null,
                     "region-code":null,
                     "region-name":null,
                     "latitude":null,
                     "longitude":null
                  },
                  "relationships":{
                     "inquiry":{
                        "data":{
                           "type":"inquiry",
                           "id":"inq_ZWRL8Dr2KjhHcewoj3meoDU3"
                        }
                     }
                  }
               },
               {
                  "type":"inquiry-session",
                  "id":"iqse_XJKSyAFd9ZuEzNXbPTSR5697",
                  "attributes":{
                     "status":"active",
                     "created-at":"2023-02-13T23:28:54.000Z",
                     "ip-address":"189.132.156.170",
                     "user-agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Mobile/15E148 Safari/604.1",
                     "os-name":"iOS",
                     "os-full-version":"14.5",
                     "device-type":"smartphone",
                     "device-name":"iPhone",
                     "browser-name":"Mobile Safari",
                     "browser-full-version":"14.1",
                     "mobile-sdk-name":null,
                     "mobile-sdk-full-version":null,
                     "is-proxy":false,
                     "is-tor":false,
                     "is-datacenter":false,
                     "threat-level":"low",
                     "country-code":"US",
                     "country-name":"United States",
                     "region-code":"CA",
                     "region-name":"California",
                     "latitude":37.751,
                     "longitude":-97.822
                  },
                  "relationships":{
                     "inquiry":{
                        "data":{
                           "type":"inquiry",
                           "id":"inq_ZWRL8Dr2KjhHcewoj3meoDU3"
                        }
                     }
                  }
               }
            ]
         },
         "created-at":"2023-02-13T23:29:15.893Z"
      }
   }
}

Event - root JSON API object Each event has payload - nested JSON API object

olosegres commented 1 year ago

@lexer Hi, json that you provided violates specification, at least its 1.0 version. That is why payload property inside attributes is treated as custom property and not parsed.

But you have two ways to work with such json:

  1. Try to deserialize attributes.payload manually where you need it
  2. Make your own property mappers to handle this case automatically (how to do it is described here https://github.com/olosegres/jsona#customize)

But better will be fix the api, because it does not meet the specification (everything that currently inside payload prop shout be placed in the json root)