bitrinjani / castable

Castable library sanitizes dirty external data by casting all properties at runtime to the types specified at compile time.
27 stars 3 forks source link

JSON Array cast #5

Open mobilefocustoar opened 6 years ago

mobilefocustoar commented 6 years ago

How are you. I think that this library is useful.

But, I have a problem in programming. I need your help.

If the result is json object, and it is simple: { "type":"xxx", "currency":"btc", "amount":"0.0", "available":"0.0" }

class is following: export class Response extends Castable { @cast type: string; @cast currency: string; @cast amount: number; @cast available: number; }

But json response is array. There is no key. the json response is following: [{ "type":"xxx", "currency":"btc", "amount":"0.0", "available":"0.0" },{ "type":"xxx", "currency":"usd", "amount":"1.0", "available":"1.0" },{ "type":"xxx", "currency":"btc", "amount":"1", "available":"1" }]

what is class for castable? Please teach me.

Regards.

mobilefocustoar commented 6 years ago

Hi, Is my question stupid?

undrthemt commented 6 years ago

Hi. Did you create Response object like this?

const response = new Response(JSON.parse(serverResponse));

Castable don't change json responses directly.

Regards.

mobilefocustoar commented 6 years ago

Thanks for your response.

Yes, I create response like above. I want to confirm that above is right.

Regards.

mobilefocustoar commented 6 years ago

I want to know how program code when root of response is array.

Thank you.

jcmonteiro commented 1 year ago
const parsedArray = (JSON.parse(serverResponse) as Array<unknown>).map(
  (elem) => new Response(elem)
);