-
Task
-
Resolution: Unresolved
-
Normal
-
None
-
None
For reference,
- For some reason, RH Cloud seems to have its own JSON generator in lib/foreman_inventory_upload/generators/json_stream.rb. !!??
- In order to use the simple_field and object_field methods of this JSON generator properly, you must pass a second argument, last. If you don't pass last, and your field happens to be the last field in a object, the stream will include an extra comma and thus stop being valid JSON.
- In order to be able to use the fact_values method, you have to add the fact names to self.fact_names in lib/foreman_inventory_upload/generators/queries.rb. If a fact name returns nil, simple_field will just return and skip that line. This causes problems with no. 2 above, because you never know for sure if a field will be the last field. (I'm guessing this is the reason for the comma method.
he reason we have our own implementation for JSON is because the standard one does not have a notion of streaming. If we use the regular JSON, it will create a ton of objects in Ruby's heap, which is not desired, especially if you are working with large JSONs. This is also the reason that we have the comma and last methods - we need to know if we should write the comma into the string, or is it the last object, and hence the comma is not needed.
If you have a better idea of last object detection and emission, I would really appreciate it.
My way to deal with the last parameter was to put the fields that we're not sure that they will appear before a field that is always written. This way we can be sure that the always written field is the last one. It's not bulletproof, but it worked till now.