summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-03-27 14:56:05 +0000
committerMichael Williamson <mike@zwobble.org>2014-03-27 14:56:05 +0000
commit72f52a18069c2a04b9cb8bbe6f5185fe4c384be1 (patch)
tree86e423c0b1592b66d8954cbb8de413f636c9017e
parentc09350f5016796ee76d2e43f174cce2fdb672ddd (diff)
Replace raw_input with the text argument
-rw-r--r--README.md4
-rw-r--r--jq.pyx11
-rw-r--r--tests/jq_tests.py10
3 files changed, 16 insertions, 9 deletions
diff --git a/README.md b/README.md
index dd1342d..16bfe8b 100644
--- a/README.md
+++ b/README.md
@@ -12,10 +12,10 @@ from jq import jq
jq(".").transform("42") == "42"
```
-The `raw_input` argument can be used to treat the input as a raw JSON string:
+If the value is unparsed JSON text, pass it in using the `text` argument:
```python
-jq(".").transform("42", raw_input=True) == 42
+jq(".").transform(text="42") == 42
```
The `raw_output` argument can be used to serialise the output into a JSON string:
diff --git a/jq.pyx b/jq.pyx
index 980662d..cec6e69 100644
--- a/jq.pyx
+++ b/jq.pyx
@@ -100,6 +100,11 @@ cdef class _ErrorStore(object):
self._errors = []
+class EmptyValue(object):
+ pass
+
+_NO_VALUE = EmptyValue()
+
cdef class _Program(object):
cdef jq_state* _jq
cdef _ErrorStore _error_store
@@ -107,8 +112,10 @@ cdef class _Program(object):
def __dealloc__(self):
jq_teardown(&self._jq)
- def transform(self, input, raw_input=False, raw_output=False, multiple_output=False):
- string_input = input if raw_input else json.dumps(input)
+ def transform(self, value=_NO_VALUE, text=_NO_VALUE, raw_output=False, multiple_output=False):
+ if (value is _NO_VALUE) == (text is _NO_VALUE):
+ raise ValueError("Either the value or text argument should be set")
+ string_input = text if text is not _NO_VALUE else json.dumps(value)
bytes_input = string_input.encode("utf8")
self._error_store.clear()
diff --git a/tests/jq_tests.py b/tests/jq_tests.py
index 0a3aca7..daa7f85 100644
--- a/tests/jq_tests.py
+++ b/tests/jq_tests.py
@@ -25,7 +25,7 @@ def can_add_one_to_each_element_of_an_array():
def input_string_is_parsed_to_json_if_raw_input_is_true():
assert_equal(
42,
- jq(".").transform("42", raw_input=True)
+ jq(".").transform(text="42")
)
@@ -65,7 +65,7 @@ def multiple_output_elements_are_returned_if_multiple_output_is_true():
def multiple_inputs_in_raw_input_are_separated_by_newlines():
assert_equal(
[2, 3, 4],
- jq(".+1").transform("1\n2\n3", raw_input=True, multiple_output=True)
+ jq(".+1").transform(text="1\n2\n3", multiple_output=True)
)
@@ -108,7 +108,7 @@ def errors_do_not_leak_between_transformations():
def value_error_is_raised_if_input_is_not_valid_json():
program = jq(".x")
try:
- program.transform("!!", raw_input=True)
+ program.transform(text="!!")
assert False, "Expected error"
except ValueError as error:
expected_error_str = "parse error: Invalid numeric literal\n"
@@ -119,7 +119,7 @@ def value_error_is_raised_if_input_is_not_valid_json():
def unicode_strings_can_be_used_as_input():
assert_equal(
u"‽",
- jq(".").transform(u'"‽"', raw_input=True)
+ jq(".").transform(text=u'"‽"')
)
@@ -127,5 +127,5 @@ def unicode_strings_can_be_used_as_input():
def unicode_strings_can_be_used_as_programs():
assert_equal(
u"Dragon‽",
- jq(u'.+"‽"').transform(u'"Dragon"', raw_input=True)
+ jq(u'.+"‽"').transform(text=u'"Dragon"')
)