Fix `PhutilReadableSerializer` to quote strings.

Summary: See D9622#inline-37279 for explanation.

Test Plan: Modified and ran unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D9632
Add a native JSON parser.

Summary: Ref T5297. Add [[https://github.com/Seldaek/jsonlint | JsonLint]] as an external library and provide `PhutilJSONParser` as a wrapper around the library's functionality.

Test Plan: Wrote some basic unit tests and ran `arc unit`.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Maniphest Tasks: T5297

Differential Revision: https://secure.phabricator.com/D9622
Modify the JsonLint library to return `array` instead of `stdClass`.

Summary:
Summary :Ref T5297. Make `PhutilJSONParser::parse($json)` roughly approximate to `json_decode($json, true)`. Generally when we parse JSON, we want the result to be an `arrray`, not a `stdClass` object.

Depends on D9622.

Test Plan: Wrote a few additional test cases.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Maniphest Tasks: T5297

Differential Revision: https://secure.phabricator.com/D9623
Fix the handling of empty keys in `PhutilJSONParser`.

Summary:
Because the empty string is not a valid property of `stdClass`, the original [[https://github.com/Seldaek/jsonlint | JsonLint]] library replaced empty keys with `'_empty_'`. After D9623, all instances of `stdClass` have been replaced by arrays, so we can remove this nonsense.

Depends on D9623.

Test Plan: Added a test case and ran `arc unit`.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D9633
Throw better exceptions from `PhutilJSONParser`.

Summary:
Ref T5297. Retrieve error context from `JsonLintParsingException` to provide a better exception message.

Depends on D9623.

Test Plan: Wrote and executed unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Maniphest Tasks: T5297

Differential Revision: https://secure.phabricator.com/D9624
Add an optional mode for `PhutilJSONParser` to detect duplicate keys

Summary:
Ref T5297. Change `PhutilJSONParser` to disallow duplicate keys by default. In addition, add an optional mode to //allow// duplicate keys. One important caveat here is that `PhutilJSONParser` will return the //first// value in the case of a duplicate, whereas `json_decode` will return the last value.

Depends on D9624.

Test Plan: Wrote and executed unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Maniphest Tasks: T5297

Differential Revision: https://secure.phabricator.com/D9627
Minor fix for `JsonLintJsonLinter` class.

Summary: Remove a `\` character, which will probably cause issues on PHP versions earlier than 5.3.

Test Plan: Thought long and hard.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D9636
Add more unit tests for `PhutilJSONParser`.

Summary: Added some additional unit tests taken from the [[https://github.com/Seldaek/jsonlint | original source]].

Test Plan: Ran `arc unit`.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D9638
Modify `phutil_json_decode` to use `PhutilJSONParser`.

Summary: Ref T5297. Modify `phutil_json_decode` to use `PhutilJSONParser` instead of the native `json_decode`.

Test Plan: Ran `arc unit`.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Maniphest Tasks: T5297

Differential Revision: https://secure.phabricator.com/D9634
Update callsites of `phutil_json_decode`.

Summary: Depends on D9634. `phutil_json_decode` now throws an exception on invalid JSON.

Test Plan: Not really sure how to test this?

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D9639
Provide more flexible string truncation in libphutil

Summary:
Ref T3307. We sometimes need to truncate strings to a given length for display, storage, or for other reasons.

Currently, `phutil_utf8_shorten()` can do this for display, but because it handles combining characters it doesn't actually impose any storage limits on the result: if the input is `o` and 50 megabytes of combining umlauts, the output will be the same as the input, since this is only "1 display character" long.

Instead, allow truncation to specify maximum byte, codepoint (utf8 character) and glyph (display character) limits.

Test Plan: Added more unit tests.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T3307

Differential Revision: https://secure.phabricator.com/D9368
Fall back to slow UTF8 algorithms that don't crash

Summary:
Fixes T5316. PCRE can blow stacks on `(x|y)+` regexps, and a user is hitting a consistent fatal in PCRE.

Rewrite the fallback for `phutil_is_utf8()` to be iterative. This is somewhat slower, but not normally too horrible, and we have a variety of ways to mitigate that if it arises as a real issue (encourage installation of mb extension, ship a fast C extension, etc).

We already have most of this code anyway.

Test Plan: We have fairly good test coverage here already, I mostly just realigned the existing test coverage to explicitly call the new code.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: webframp, epriestley

Maniphest Tasks: T5316

Differential Revision: https://secure.phabricator.com/D9472