[ruby-core:113736] [Ruby master Feature#19706] Make JSON.[] support ARGF object
From:
"nobu (Nobuyoshi Nakada) via ruby-core" <ruby-core@...>
Date:
2023-06-02 02:28:14 UTC
List:
ruby-core #113736
Issue #19706 has been updated by nobu (Nobuyoshi Nakada).
Status changed from Open to Third Party's Issue
Json has its own upstream, https://github.com/flori/json.
BTW: I've never used `JSON[]`, but it looks like whole opposite functions are crammed into one method.
----------------------------------------
Feature #19706: Make JSON.[] support ARGF object
https://bugs.ruby-lang.org/issues/19706#change-103385
* Author: tatzyr (Tatsuya Otsuka)
* Status: Third Party's Issue
* Priority: Normal
----------------------------------------
**Abstract**
I propose to extend the `JSON.[]` method in the `json` standard library to support `ARGF` object directly.
**Background**
Currently, the `JSON.[]` method supports parsing from a string or generating a string from an object. However, it does not directly support `ARGF`, which is often used to handle command-line input. With this extension, `ARGF` object can be effectively used with the `JSON.[]` method to parse JSON from command-line input.
**Proposal**
This proposal aims to provide a more concise way to handle JSON input than the current method, `JSON.parse(ARGF.read)`. An example usage would be: `echo '{"foo": "bar"}' | ruby -rjson -e 'puts JSON[ARGF]["foo"]'`. This will enable developers to parse JSON in a similar way to how the `jq` utility works.
**Implementation**
Here's the proposed change to `JSON.[]`:
```diff
### ext/json/lib/json/common.rb
module JSON
class << self
def [](object, opts = {})
if object.respond_to? :to_str
JSON.parse(object.to_str, opts)
+ elsif object.is_a?(ARGF.class)
+ JSON.parse(object.read, opts)
else
JSON.generate(object, opts)
end
end
end
end
```
This change checks whether the given `object` is an instance of `ARGF.class` and, if so, reads from it and parses the result as JSON. Other options like `object.respond_to? :read` or `object.is_a?(IO)` were considered, but were not chosen due to significant implications for backward compatibility.
**Evaluation & Discussion**
While this enhancement does not alter the existing behavior of `JSON.[]` for types of objects it currently handles, it would change the behavior when `ARGF` is used. This change may not be fully backward-compatible. However, it significantly simplifies the code required to parse JSON from the command line, which is a common use case.
One crucial aspect to note is that the result of `JSON[ARGF]` will change due to this modification. Currently, it returns the string `"\"ARGF\""`, but with this proposed change, it would parse the JSON content read from `ARGF` and return a Hash.
**Summary**
By extending `JSON.[]` method to directly support `ARGF`, developers will be able to more concisely handle JSON input from command line. This change, while not fully backward-compatible, will make `JSON.[]` more suitable for use cases involving command line input, thereby simplifying the required code.
I look forward to hearing your thoughts on this proposal.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/