/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ export namespace ObjectSerializer { export type BodyType = string | ReadableStream | Blob ; export function serialize ( data , type , getJsonObj ? ) { if ( getJsonObj ) { if ( isList ( type ) ) { return JSON . stringify ( data . map ( ( item ) => { return getJsonObj ( item ) ; } ) ) ; } else if ( isMap ( type ) ) { const obj = { } ; Object . keys ( data ) . forEach ( ( key ) => { obj [ key ] = getJsonObj ( data [ key ] ) ; } ) ; return JSON . stringify ( obj ) ; } else { return JSON . stringify ( getJsonObj ( data ) ) ; } } return JSON . stringify ( data ) ; } export function deserialize < T > ( data , type ? , bodyModel ? ) { if ( typeof bodyModel === "object" ) { if ( isList ( type ) ) { return data . map ( ( item ) => { return bodyModel . getDeserializedJsonObj ( item ) ; } ) ; } else if ( isMap ( type ) ) { const obj = { } ; Object . keys ( data ) . forEach ( ( key ) => { obj [ key ] = bodyModel . getDeserializedJsonObj ( data [ key ] ) ; } ) ; return obj as T ; } else { return bodyModel . getDeserializedJsonObj ( data ) ; } } return data ; } function isList ( type ) { return type . lastIndexOf ( "Array<" , 0 ) === 0 ; } const mapRegex = new RegExp ( "^{\\[\\w*:\\w*]:model.\\w*}$" ) ; function isMap ( type ) { return mapRegex . test ( type . replace ( /\s/g , "" ) ) ; } }