GillianPerard/typescript-json-serializer

[QUESTION]: Custom Serialization/Deserialization - Is it possible to convert snake case to camel case?

dinbtechit opened this issue · 1 comments

Description

Custom Serialization/Deserialization - Is it possible to convert the snake case (first_name) to a camel case (firstName)?

Example Json for serializaton

{
  "modes": [
       "field_1" : "",
       "field_second" : "",
        "messages" : [
              "field_last": ""
        ]
  ]
}

CustomSerializer: (from the example)

// Or you can instantiate a serializer with your custom options
const customSerializer = new JsonSerializer({
    // Throw errors instead of logging
    errorCallback: throwError,

    // Allow all nullish values
    nullishPolicy: {
        undefined: 'allow',
        null: 'allow'
    };

    // e.g. if all the properties in the json object are prefixed by '_'
    formatPropertyName: (propertyName: string) => `_${propertyName}`; 
})

Proposed solution

Expected Output:

{
   modes: [
     field1: '',
     fieldSecond: '',
     messages: [
         fieldLast: ''
     ]
   ]
}

Hi, as you see in the example you can pass your own function to formatPropertyName option when you instantiate the serializer.

So, in your case you need to pass a function that returns a snake-cased property name. You could use libs for that (change-case, to-snake-case, ...) or create your own one.