krasa / StringManipulation

IntelliJ plugin - https://plugins.jetbrains.com/plugin/2162
Apache License 2.0
694 stars 81 forks source link

When converting from YAML to JSON with the `Convert Between JSON and YAML` action, the format of dates and timestamps is not preserved. #188

Closed binoternary closed 1 year ago

binoternary commented 1 year ago

Describe the bug When converting from YAML to JSON with the Convert Between JSON and YAML action, the format of dates and timestamps is not preserved.

To Reproduce

  1. Create YAML file with the contents
    
    foo:
    bar: 2022-09-15T12:15:30.123Z
    baz: 2022-09-15
2. Run the action `Convert Between JSON and YAML` on that file.
4. The JSON result is

{ "foo": { "bar": "Sep 15, 2022, 3:15:30 PM", "baz": "Sep 15, 2022, 3:00:00 AM" } }


**Expected behavior**
The ISO 8601 format is preserved and the output would be

{ "foo": { "bar": "2022-09-15T12:15:30.123Z", "baz": "2022-09-15" } }


**Environment :**

IntelliJ IDEA 2022.2.2 (Ultimate Edition) Build #IU-222.4167.29, built on September 13, 2022 Licensed to [...] Subscription is active until [...]. Runtime version: 17.0.4+7-b469.53 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Linux 5.4.0-122-generic GC: G1 Young Generation, G1 Old Generation Memory: 4096M Cores: 8 Registry: debugger.watches.in.variables=false

Non-Bundled Plugins: net.sjrx.intellij.plugins.systemdunitfiles (0.3.9) org.jetbrains.plugins.hocon (2022.1.0) String Manipulation (9.5.1)

Kotlin: 222-1.7.10-release-334-IJ4167.29 Current Desktop: ubuntu:GNOME

krasa commented 1 year ago

That seems not possible, without a complete re-implementation. The format gets lost when converting from yaml to an object.

But there could be a date format settings as a workaround.

@m-radzikowski opinion?

m-radzikowski commented 1 year ago

Interesting.

Conversion is done with two libraries, snakeyaml and gson. When we parse the YAML input without providing the input type, snakeyaml tries to be clever and detect types. From the docs:

Implicit types When a tag for a scalar node is not explicitly defined, SnakeYAML tries to detect the type applying regular expressions to the content of the scalar node. 1.0 -> Float 42 -> Integer 2009-03-30 -> Date

But this behavior can be overridden by implementing a custom resolver, like this.

Let me try it and I will submit a fix.