I’m receiving values in JSON from an API, which can be erroneous because of legacy code that apparently won’t be fixed. These values are being deserialized pretty simply by casting the JSON to an interface. This is all well and good. The problem is I’d like to fix those erroneous values straight at the source during deserialization so that I don’t have to deal with them. An example to illustrate:
enum Bar {
bar = "bar",
qux = "qux",
}
// Assume we have multiple interfaces using Bar such that it'd be
// better for the serialization to be defined in Bar
interface Data1 {
foo: Bar[];
}
interface Data2 {
foo: Bar[];
}
// All data fetching is done using generics such that changing
// it would need a full rework
function fetchData<T>() {
// We expect bar but sometimes receive baz, which would be better deserialized as bar
// {"foo": [ "bar", "baz", "qux"]}
const response = await fetch("data.json");
return await response.json() as T;
}
const data: Data1 = fetchData<Data1>();
I unfortunately can’t define the enum as having multiple values for the same key. I could make different interfaces for what we receive and what we actually work with so that the data is transformed properly but that would require a rework of many things so I’m looking for a way to only affect that specific enum deserialization.
2
Answers
You can create a function to intercept and clean up the JSON data as it’s being parsed and this way you can fix erroneous values at the source-
You will have to define a customDeserializer that accepts json string and a custom transformation function e.g.
Then you can initialize data1 and data2 using customDeserializer instead of JSON.parse
I tried this in the typescript-playground