JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
958 stars 169 forks source link

Evaluate also getter from classes #177

Open WebDucer opened 2 years ago

WebDucer commented 2 years ago

Could be related to https://github.com/JSONPath-Plus/JSONPath/issues/174

Motivation

Currently the getter from classes, that encapsulate fields, are not "visible" to the library, so it is not possible to get a value from getter property. Becase getter are "seen" from developer as readonly fields, the library should at least also be able to "read" them.

Current behavior

getter are ignored

Desired behavior

The getters should be evaluated exactly the same as pure fields in classes.

Code example

import { JSONPath } from 'jsonpath-plus';

class TestData {
  #value;

  constructor(value) {
    this.#value = value;
  }

  get value() {
    return this.#value;
  }

  toJSON() {
    return { jsonValue: this.value };
  }

  someValue = 33;
}

const data = new TestData(11);
console.log(data); // TestData {someValue: 33}
console.log(data.value); // 11
console.log(data['value']); // 11
console.log(JSON.stringify(data)); // {"jsonValue":11}

const val = JSONPath({ path: '$.value', json: data });
const val1 = JSONPath({ path: '$.jsonValue', json: data });
const val2 = JSONPath({ path: '$.#value', json: data });
const val3 = JSONPath({ path: '$.someValue', json: data });

console.log(val); // [] - expected [11]
console.log(val1); // []
console.log(val2); // []
console.log(val3); // [33]

console.log(Object.prototype.hasOwnProperty.call(data, 'value')); // false
console.log('value' in data); // true
LoicMahieu commented 12 months ago

Related to PR #197