Casting data from backend to typed object on frontend

Hello everyone,

I’am struggling with sending data from backend (native php) to typed structure on Frontend (js in haxe) in Haxe.

Let’s say that my backend sending data to js variable in html and main.js processing the rest…:

In var configuration.singleProductData I can reach all data from product (data showed in console from backend):

configuration.singleProductData: {
id: 1,
title: ‘New Product’
prices: {
EUR : {
price: 10,
formatted: 10,00 €
USD : {
price: 11,
price_formatted: 11,00 $
GBP : {
price: 9,
price_formatted: 9,00 Pounds

In the haxe I have got prepared structures for product and prices:

typedef ProductItem = {
public var id:Int;
public var title:String;
public var prices:Map<String,Price>;

typedef Price = {
public var price:Float;
public var priceFormatted:String;

I am trying with many ways how to parse it from native php to structured form in Haxe but still failed:
1. I need to iterate each item in object because between variables on BE and FE is sometimes different naming
2. i need to specify Iterable or Iterator - i think its should have lots real examples
3. how to iterate `prices` - its a map not array and this is also struggeling how to get into ds.Map each price (eur, usd, ...)?

Do you have any showcase for processing of these data?

Thank you so much again,

Trying also create iterator for map:

var iterator:haxe.iterators.MapKeyValueIterator = new MapKeyValueIterator(configuration.singleProductData.prices);

but got only compiler error:
characters 19-53 : Not enough type parameters for haxe.iterators.MapKeyValueIterator

Thanks for any help!


configuration.singleProductData seems to contain json object, try haxelib search json (json2object seems interesting, get info via haxelib info json).

As error message explains: you are missing type parameters, e.g.

var iterator:haxe.iterators.MapKeyValueIterator<String,Price> = new MapKeyValueIterator(…);

or use type inference instead of explicit type hint

var iterator = new MapKeyValueIterator(…)

1 Like

Ohh, Thank you. I missed so tiny thing var iterator:haxe.iterators.MapKeyValueIterator**<String,Price>**

Now I got it around iterators… All my examples works fine.

Thank you so much!

I also found solutions during iterating Object:

for(n in Reflect.fields(single_product.prices)) {
    var priceData = Reflect.field(single_product.prices, n);

I would interest which of this solutions is more more efficient or faster and why?

Reflection turns static into dynamic access on the static target, so it slow(est) solution. However, on dynamic targets like JS reflection should have smaller overload, however it is always better to check generated code.

Fastest solution could be for (key => val in map) because of the optimizations, but don’t take my words for granted because I haven’t checked generated code for long time.

1 Like

Map is not the same as an object with dynamic fields (in PHP terms, think of the former as array and the latter as stdClass).

Instead of Map<String, Price> you can use haxe.DynamicAccess<Price> and it will work as expected, with iterators and all.

1 Like