Closed hackerhelmut closed 8 years ago
Hi,
Thanks for interest in ObjectPath! :)
To better understand your PR could you tell me if is it possible to do a 1:1 conversion from YAML to JSON? If it isn't then why?
Regards, Adrian
Hi Adrian,
sorry for the late answer. But I really like your ObjectPath and wanted to thake the proper time to answer when I had time.
As I understand it YAML is a superset of JSON. So JSON itself is valid YAML syntax. But YAML can be written much simpler.
For example the following JSON is valid YAML but can be expressed as the following YAML block as well.
{
"foo": {
"bar": [
5, 4,
null
]
}
}
---
foo:
bar:
- 5
- 4
- ~
Moreover it is possible to set anchors and references in a YAML document. So there is no need to duplicate code:
---
foo:
bar: &values
a: 5
b: 4
qux: *values
So obj["foo"]["qux"]["a"]
equals 5
as well. But this changes nothing in the underlying data structures. In the default case it is still only primitives, (ordered
)dict
s and list
s like in JSON.
But you could also use tags which can define a type for the following statements, for example:
---
event:
start: !date 2015-12-16T16:20:00.00+02:00
name: My Example Presentation
attachment: !file
name: MyPresentation # name of the file
data: AAABSK...XXDF== # base64 encoded file content
So here !date
and !file
express an explicit type. A YAML parser reads in the information and tries to convert the following values to the correct type if registered. Most YAML parser have even some intern types already registered (e.g. !!python/bytes
docs). These types are not JSON compatible and therefore special support is needed to parse them.
So I had a look in your code and figured you already treat any unknown object in the hierarchal path as an object and call __getattribute__
on it so it was only logical to extend this mechanism to support a user defined handler which enables an developer to write self defined handler functions for their classes.
Furthermore YAML is capable of complex object keys. But since this is not possible in Python it is not usable here.
Best regards Rolf
Hello,
this pull request enables the API user to define a callback handler helping the handling of python classes derived from object. For example I use ObjectPath to navigate through an document generated from a YAML description. In YAML it is possible to define "tags" corresponding to Python classes. This callback function enables me to identify the type of the class and return a dict or list for further path resolution.
Best regards Rolf