Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116016 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 52274 invoked from network); 8 Sep 2021 20:10:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2021 20:10:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 353D81804AD for ; Wed, 8 Sep 2021 13:48:37 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 8 Sep 2021 13:48:36 -0700 (PDT) Received: by mail-pg1-f176.google.com with SMTP id g184so3890901pgc.6 for ; Wed, 08 Sep 2021 13:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WLA+FsFcEMufi9WCHajCwzdolxOMAFBS9BndkyLcf7U=; b=vDYvwP+QeHWmPMNmnEtsmMQwq4QkMU1HYoPFJKNXIz0/rH3pdrvHFksaYyVWdKBa3l rxqrrRREoStz4m/yi67BPNkhx00pzKytTTZPHsB8S1fI0qXJ+p6Hu6bnupTq+jTOB3yM pQQQ31pCKGF800Z4xxz6O2k1psiE2PTtU9K//s45luVU1lA15+/Wsx+SXcD4pbtiKy/r Se9ZhTPWuYgpOnb7Fj+SqgjdNCG50W/l6mMcw5PCvP7iCwQMvvuYyb2ga/SSuBiDvWJC jz0oo/T/LCn+X7alDgu2ZHMD9zr0Hq1H7BIbGsLo8/1owptwn7VTbxGCFWXvHCWqIv9V Ibwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WLA+FsFcEMufi9WCHajCwzdolxOMAFBS9BndkyLcf7U=; b=4wnozbgOboGmg8kK1XMeY7tnr8cgsVDrlxdm6FYr/tA3+372qEqxX/jAcIHc96QjtG knYuuBcVQ/vCDC/Ilsyby2Q8gT0XiJpG29ftXhg4ENhNNMqRBVSQuTIWEDuqkh9UVxFk 2lPFXkcENJ0rjiR902ksmZ6KRy5PpXT+wJ2BZ9fnszR7W8mJNZ1hvGfJZtTMo7lRpsA5 crgM199UWVhC9Pj2EL2cIeSWXDp+eNfD6TG4gEqKzyRaguHQGF/ltuhlYI7zWvHmG/J5 ZtxCs4+jPgYQXQRcT0dQd07BYNmCt/5KLMCOmEIUsIV2k0bhPPWjRA63vhI4tTX8Ozb9 B14Q== X-Gm-Message-State: AOAM530f/8hSFEppGmp2ay4XdriTRGKyvE73EU6n54+MTPwwQ3iBbhRS QR1eCApy+9ATNJtFqnmOgkQcIafTL9fsBNK9WLTs6g== X-Google-Smtp-Source: ABdhPJxK4LtYfK5njLSdPFd5/TJPZ5tQh4xyIalzOkcn/owztNMm5DH0sdpNucF7txX4GXb7d7g1+9za+D2OcyTqqAA= X-Received: by 2002:a62:798d:0:b0:3ef:72a4:e899 with SMTP id u135-20020a62798d000000b003ef72a4e899mr239756pfc.13.1631134113305; Wed, 08 Sep 2021 13:48:33 -0700 (PDT) MIME-Version: 1.0 References: <46f2e842-8608-0622-f1c1-3eb746658c84@gmail.com> In-Reply-To: Date: Wed, 8 Sep 2021 22:48:22 +0200 Message-ID: To: Lynn Cc: Mike Schinkel , Rowan Tommins , PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Alias stdClass to DynamicObject? From: andreas@dqxtech.net (Andreas Hennings) Wow! I notice that ArrayObject already does everything we would need for json_encode(). assert(json_encode(new ArrayObject([5])) === '{"0":5}'); However, it does so in a very strange way, not using any of the public methods, but also not using dynamic properties. It seems there is a hard-coded internal implementation when calling json_encode() or converting to array. https://3v4l.org/rAc4K I think we would want something more clean and transparent. On Wed, 8 Sept 2021 at 21:58, Andreas Hennings wrote: > > On Wed, 8 Sept 2021 at 18:33, Lynn wrote: > > > > On Wed, Sep 8, 2021 at 5:38 PM Mike Schinkel wrote: > > > > > A couple more things; add a `JSON_OUTPUT_DYNAMIC_OBJECT` flag to output to > > > DynamicObject for `json_decode()`, add a 3rd parameter for flags to > > > var_export() for the same reason, a `'return_dynamic_object'` option for > > > `unserialize()`, and so on. > > > > > > > It would also be interesting to enter a user-defined class here to work as > > a reversed JsonSerializable. Could be a static factory method for all I > > care, and would omit the requirement of serialization libraries for > > "simple" things where you still want decent object typing. Could also work > > together with interfaces accepting properties, effectively making the > > result of json_decode an anonymous class that still adheres to an > > interface. In case of the custom 'deserialization' you can throw exceptions > > if the format is not correct. In the case of an anonymous class with an > > interface it could throw an exception if the structure doesn't match > > (possibly controlled by flags?). > > I think we want round-trips of json_decode() + json_encode() to work > loss-free, without any custom classes or extra parameters. > > Currently, '{}' and '[]' have different results with json_decode(). > > assert(json_encode(json_decode('{}')) === '{}'); // Perfect round-trip. > assert(json_encode(json_decode('{}', JSON_OBJECT_AS_ARRAY)) === '[]'); > // Lossy round-trip. > > This can only work if we have a built-in data transfer object, > distinct from arrays. > Currently this is \stdClass, using the dynamic property mechanism. > > But one could easily imagine a different kind of data transfer object, > which would use a different mechanism instead of dynamic properties. > > A native implementation of JsonSerializable does not really work here, > because ->jsonSerialize() would have to return \stdClass to result in > '{}'. > Instead, what about something with a "->getData(): array" method, but > then json_decode() would encode it as '{}'? > > assert(json_decode('{}') instanceof DataTransferObject); > assert(json_decode('{}')->getData() === []); > assert(json_encode(json_decode('{}')->getData()) === '[]'); > assert(json_encode(json_decode('{}')) === '{}'); > > For the proposed rename: > - If we can fully deprecate and replace dynamic properties long-term, > I would rather keep the name \stdClass until it dies. > - Instead of an alias or rename, I would rather have a new data > transfer object class which would not rely on dynamic properties, but > on a new mechanism. > > Imo, creating an alias won't actually make life easier for anyone, > unless we can fully remove and replace \stdClass. > It would mean that developers need to learn both names, and understand > how aliases work - something you don't really need to learn otherwise. > Think of the silly wtfs that can occur if people don't fully > understand aliases, or are not aware that DynamicObject is just an > alias, while \ReflectionClass and get_class() still return 'stdClass' > as the class name. > > -- Andreas