On Wednesday, 19 February 2020 at 08:47:04 UTC, Petar Kirov [ZombineDev] wrote:
On Wednesday, 19 February 2020 at 08:14:34 UTC, AlphaPurned wrote:

The first is std.json. It is broke. Doesn't work with tuples. The change above fixes it by treating tuple as an array(same code). It works fine.

Can you post a minimal, but complete program that shows the problems with std.json regarding tuples?

If you do we could open a pull request that fixes the problem and also uses the code of your program as a unit test, to both showcase the support for tuples and also prevent regressions in the future.

It's in the code. Don't n eed a minimal example. If you want one just try to json a tuple.
...

else static if (isArray!T || (T.stringof.length > 4 && T.stringof[0..5] == "Tuple"))
        {
            type_tag = JSONType.array;
            static if (is(ElementEncodingType!T : JSONValue))
            {
                JSONValue[] t = arg;
                () @trusted { store.array = t; }();
            }
            else
            {
                JSONValue[] new_arg = new JSONValue[arg.length];
                foreach (i, e; arg)
                    new_arg[i] = JSONValue(e);
                () @trusted { store.array = new_arg; }();
            }
        }
        else static if (is(T : JSONValue))
        {
            type_tag = arg.type;
            store = arg.store;
        }
        else
        {
static assert(false, text(`unable to convert type "`, T.stringof, `" to json`));
        }

That is the fix.

The code does not check for tuples. I simply modified the check on arrays to check for tuples since the static code is exactly the same for arrays and tuples(using indexing).


This is not complicated. Someone that knows about the json code should do it.

The stringof bug exists too. There is no Stringof.

import std, std.typecons;
void main()
{
    auto X = "asdf";
    writeln(JSONValue(X).toString);
        auto Y = tuple(3.4, "asdf");
    writeln(JSONValue(Y).toString);
}



/dlang/dmd/linux/bin64/../../src/phobos/std/json.d(530): Error: no property Stringof for type std.typecons.Tuple!(double, string) /dlang/dmd/linux/bin64/../../src/phobos/std/json.d(530): Error: static assert: __error /dlang/dmd/linux/bin64/../../src/phobos/std/json.d(564): instantiated from here: assign!(Tuple!(double, string)) onlineapp.d(7): instantiated from here: __ctor!(Tuple!(double, string))


Fixing the Stringof bug won't make it work with tuple though, the code I added fixes this though, again, as I've said plenty of times, I just made it treat tuples like arrays.

Reply via email to