0

I have a response from API which looks like a JSON, But it isn't.

[ { detection:
 FaceDetection {
   _imageDims: [Dimensions],
   _score: 0.8957952857017517,
   _classScore: 0.8957952857017517,
   _className: '',
   _box: [Box] },
landmarks:
 FaceLandmarks68 { _imgDims: [Dimensions], _shift: [Point], _positions: [Array] },
unshiftedLandmarks:
 FaceLandmarks68 { _imgDims: [Dimensions], _shift: [Point], _positions: [Array] },
alignedRect:
 FaceDetection {
   _imageDims: [Dimensions],
   _score: 0.8957952857017517,
   _classScore: 0.8957952857017517,
   _className: '',
   _box: [Box] },
descriptor:
 Float32Array [
   -0.1426914483308792,
   0.08866042643785477,
]

As you can see the face detection is not a key-value pair but a function. ( This understood from here: No colon after property name in object declaration, is it valid? ).But when I try to serialize this, it will avoid those functions and only take the key-value pair. So how should I serialize this without losing anything?

Things I have tried so far is, JSON.stringify(), which obviously don't work. Other is an NPM package https://www.npmjs.com/package/serialize-javascript

So how should I serialize this object without losing functions inside?

md-shah
  • 375
  • 4
  • 17
  • you have a javascript object and you want to turn it into a json string,what will you do with that json string? – madalinivascu Feb 14 '19 at 06:06
  • Why would you wanna send a function as a response? I doubt you can even do that. – varun agarwal Feb 14 '19 at 06:07
  • I want this to be used in other API. So need to store this in DB. MongoDB only supports JSON. So store serialized response then deserialize the response then use it in the second API. – md-shah Feb 14 '19 at 06:12
  • You cannot serialise functions into JSON. What you can do is create a new object and pass the (deserialised) JSON data to it upon initialisation. – VLAZ Feb 14 '19 at 06:20
  • Try to encode it in Base64 – Ayan_84 Feb 14 '19 at 06:28

1 Answers1

0

If you control that API endpoint, simply do not do this. If you don't, then maybe you should ask whoever came up with this, how they meant it to be used (there definitely should be tools that can/know how to work with such format). If you can't do either, then API response is the string, take it as a whole, keep the format and save it however you like (file, database, etc).

jayarjo
  • 16,124
  • 24
  • 94
  • 138
  • Its a response from library - Face-API.js – md-shah Feb 14 '19 at 06:26
  • What's the library, can you link it? – jayarjo Feb 14 '19 at 06:27
  • So you are trying to serialize an actual tree of living breathing JavaScript objects? That's not possible. There should be a layer that knows how to convert an object with methods to a string and then how to do the opposite. Either the library should prepare it's classes for such, or someone else (you maybe) should figure out the structure and write the own implementation. But there's no generic solution for this, every library is different, so it has to be specialized. – jayarjo Feb 14 '19 at 06:39