Using javascript native Reflect is one option. But maybe you can use
Object.keys and Object.getOwnPropertyDescriptor to keep compatible with
IE11 ?

Royale reflection is 'heavier' but gives you more specific support from
within the framework.

Probably the easiest way to see how to use that is via examples:

https://github.com/apache/royale-asjs/blob/develop/manualtests/UnitTests/src/main/royale/flexUnitTests/reflection/ReflectionTesterTest.as


for a public var on an instance

var definition:TypeDefinition = describeType(inst);

var variables:Array = definition.variables;
the VariableDefinition instances in this Array let you get and set the
value on an instance

var varDef:VariableDefinition = variables[0];
trace(varDef.name)
trace(varDef.getValue(inst))

specific example:
https://github.com/apache/royale-asjs/blob/1b905918ca30b3280e8b90ad7ee73ff6193b781d/manualtests/UnitTests/src/main/royale/flexUnitTests/reflection/ReflectionTesterTest.as#L237

So the above does provide you with a way to filter public vars (and also
public accessors as well) on an instance, and you can get and set the
values. This avoids all the private vars and any other namespaces that may
appear in native enumeration (reflection).

If there is not already some JSON support for public vars and avoiding
private vars etc, then it could definitely be supported with a replacer
function that uses royale reflection. Using reflection classes from the
framework could be a starting point, but probably using the reflection data
directly would be the better solution. AMFBinaryData uses this extensively
for AMF serialization, doing that with JSON would be a similar thing.












On Fri, Mar 29, 2019 at 8:16 AM Olaf Krueger <[email protected]> wrote:

> Hi,
>
> > But reflection classes now support it...
>
> Just to mention it:
> Thanks to Piotr we're using Reflection [1] to extract the getters in order
> create dynamic objects which can be stringified by using JSON.stringify()
> then.
>
> However, would be even better if Yishay's idea or something other on the
> framework side would provide solutions. I could imagine that others will
> also run into that issue.
>
> Olaf
>
> [1]
> public static function getGetters(obj:Object):Array
> {
>     return Reflect.ownKeys(obj).filter(
>         function(name:*):* {
>               var dummy:* = typeof Reflect.getOwnPropertyDescriptor(obj,
> name)["get"] === "function";
>               return dummy;
>         });
> }
>
>
>
> --
> Sent from: http://apache-royale-development.20373.n8.nabble.com/
>

Reply via email to