Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114702 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11145 invoked from network); 3 Jun 2021 00:32:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jun 2021 00:32:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2A8111804CC for ; Wed, 2 Jun 2021 17:45:59 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) (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, 2 Jun 2021 17:45:58 -0700 (PDT) Received: by mail-oi1-f171.google.com with SMTP id c3so4565031oic.8 for ; Wed, 02 Jun 2021 17:45:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iR3VddwsOWR7u6uS6yVzrwIpVRiDfYSwUhMN8B7a7cw=; b=TpOa6k4CnboEMpjQN5W/8qMoioy8gld4HXNzHofPqVOt9EzwXzIhaQA+m9rb7W2R2P CYTvYZiw+0SuoBSf+Wur8CUXqMYXSCce220+VP9b70ISj/a3dwHdj8BcNoymnpKeAz7w g8ra917IGLZkm+3tCaAUDwSFIVMpXyi/l+oxyj6HgKQIxmZkJExnx5LIbUT1cgoGF82f 1CawxiP4wrsZch9XKAiG/eWtN0viOGIzEEafTHh2vHhJn3Lusggm4wyZuKPpDUNiWI0v I2MhoDhNZlrdEXUh9gEvl/OcIU5pqo1+ZmtaB/Y/TZQHdh66a3DKVuKX/0phYfBGsqid NHgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iR3VddwsOWR7u6uS6yVzrwIpVRiDfYSwUhMN8B7a7cw=; b=U7cKL6eM5y4iQjOjQC0m2rZkzDS021tRCnYlhFrMnyIIxADkmlDBhvKAa+gWhhnhEb f2u+XiwVdFTs7Xa9pxZb3v1un1Uknogk9zVQ6jrMsQjxLRPjCc0YRoNgTF0sVc6t6IyP yqhsPtX1Cnr6VOERUwaDzx7LGoCQ6k3ZYoPWZKn03fBNvOVBvBFVy62hxtuyL9POsUIY vDZXnP3+LLcmGXRfcawdM/prBfjsF2TMbWQqFcctJMDi0+Q5hdNy/vQduVs2bPzmr1NQ aGRZRXa/807+lecem3kqcCSEI0NFrYXc9YpGtE2/OMUg6ulanSk/wDH8zJO1rwxuf7lG DZUQ== X-Gm-Message-State: AOAM530g+B8ti/jKKiIiw8KouNUNzL8LVhEc/JPBanDUl8KBxzzmDy9L 6QrjJCe40kx2ExxBZD3TbaFIqfyh8qjGK1mSLs4= X-Google-Smtp-Source: ABdhPJy+n8EwlVQv3vojAi5ykvfKBksV8bk7KdsN2Z6h21GMgh256ifHPEtCtrzBcrkziHcV/g00s5yDbAGf78GF4Nw= X-Received: by 2002:a54:458b:: with SMTP id z11mr22897803oib.177.1622681156451; Wed, 02 Jun 2021 17:45:56 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 3 Jun 2021 01:45:47 +0100 Message-ID: To: Timon de Groot Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000ea01fe05c3d1e568" Subject: Re: [PHP-DEV] json_encode indent parameter From: davidgebler@gmail.com (David Gebler) --000000000000ea01fe05c3d1e568 Content-Type: text/plain; charset="UTF-8" On Wed, 2 Jun 2021, 23:03 Timon de Groot, wrote: > It's not possible to tell json_encode what indentation level should be > used when using > the JSON_PRETTY_PRINT option (2, 4, 8, etc). When generating JSON files > which can be > used/read/edited by users, indentation starts to become a relevant topic. > > - JavaScript has a function called JSON.stringify wich accepts the > optional 'space' parameter > - See > > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify > > - Python has a function called json.dump which accepts the optional > 'indent' parameter > (which also controls pretty printing) > - See https://docs.python.org/3/library/json.html#basic-usage > > I would like to create an RFC draft to implement an 'indent' parameter. > Before I start a draft, I need to know if this is a good idea, so I > start with a few questions. > > 1. What do you think of adding an 'indent' parameter? > I'm not at all opposed in principle. As you've pointed out, there is both a use case and precedent in other common languages. 2. How would that parameter work when the JSON_PRETTY_PRINT option is > not passed? > Give it a default value equivalent to whatever JSON_PRETTY_PRINT spits out at the moment and silently ignore it if not formatting? 3. What do you think of adding json_encode options instead, like > JSON_PRETTY_PRINT_INDENT_2 and JSON_PRETTY_PRINT_INDENT_4? > Personally I'd avoid more magic constants. The argument in favour of this approach I suppose would be a) there are in practice a very small number of common values for indentation which tend to be used, 2 and 4 being the most common and b) there is already a third, optional depth parameter which would remain before any fourth spacing parameter. I would say though (a) is true enough but doesn't necessarily cover all use cases, adds more magic constants and doesn't provide equivalence to the JSON libraries of other common languages which permit for custom values, while (b) is less of an issue with PHP 8's named parameters. So if you do go ahead and make an RFC for this, I think my preference would be as a new parameter on json_encode accepting a positive integer. > Looking forward to you replies and feedback! > > -- > > Kind regards, > > Timon de Groot > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000ea01fe05c3d1e568--