gpt4 book ai didi

java - 使用 Spring、Scala 和 Jackson 将 JSON 反序列化为 Map[String, SomeJavaObject]

转载 作者:行者123 更新时间:2023-11-30 09:13:27 26 4
gpt4 key购买 nike

我刚刚开始尝试使用 Scala,并尝试在现有的 Java/Spring 应用程序中使用它以及 Jackson 2.3.1 和 jackson-module-scala。

我正在尝试反序列化 JSON,如下所示,以 StringColor 对象的 Map 结束,但是当我运行下面的代码,我最终得到了 StringMapMap:

{
"1A-X": {
"c": 0,
"m": 0,
"y": 0,
"k": 0,
"r": 255,
"g": 255,
"b": 255
}
}

为什么我生成的 Map 的值是另一个 Map 而不是 Color 对象?

Color 是带有 @JsonCreator 注释构造函数的现有 Java 类。

@RequestMapping(Array("/swatches"))
def setSwatches(@RequestBody swatches: Map[String, Color]) = {
println(swatches)
println("Map class " + swatches.getClass + ", key class " + swatches.get("1A-X").get.getClass)
}

将上述 JSON 传递给此代码的输出如下:

Map(1A-X -> Map(y -> 1, m -> 1, b -> 51, g -> 1, c -> 1, r -> 5, k -> 1))
Map class class scala.collection.immutable.Map$Map1, key class class scala.collection.immutable.HashMap$HashTrieMap

如果我只是直接传递一个 Color 而没有代码到 Color map,反序列化会按预期工作。

请告诉我如何成功地直接反序列化为 Map[String, Color]。

更多详情

颜色.java

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;

public final class Color {
static final Color NOCOLOR = new Color(0f, 0f, 0f, -1f, -1, -1, -1);
private final float c, m, y, k;
private final int r, g, b;

/** from string in format: CMYKcolor(0,0,0,0) or CMYKRGBcolor(c,m,y,k,r,g,b)*/
static Color valueOf(final String colorStringFormat) {
return colorStringFormat == null || colorStringFormat.isEmpty()
? NOCOLOR : new Color(colorStringFormat);
}

/** from string in format: CMYKcolor(0,0,0,0) CMYKRGBcolor(c,m,y,k,r,g,b)*/
Color(final String colorStringFormat) {
this(colorStringFormat.substring(colorStringFormat.indexOf('(') + 1,
colorStringFormat.indexOf(')')).split(","));
}

Color(final String... colors) {
this(Float.parseFloat(colors[0]), Float.parseFloat(colors[1]),
Float.parseFloat(colors[2]), Float.parseFloat(colors[3]),
(colors.length==4)?-1:Integer.parseInt(colors[4]),
(colors.length==4)?-1:Integer.parseInt(colors[5]),
(colors.length==4)?-1:Integer.parseInt(colors[6]));
}

/** all values must be between 0f and 1f inclusive */
public Color(final float c, final float m, final float y, final float k) {
this(c, m, y, k, -1, -1, -1);
}

/** cmyk values must be between 0f and 1f inclusive; rgb values must be between 0 and 255 inclusive */
@JsonCreator
public Color(@JsonProperty("c") final float c, @JsonProperty("m") final float m, @JsonProperty("y") final float y,
@JsonProperty("k") final float k, @JsonProperty("r") final int r, @JsonProperty("g") final int g,
@JsonProperty("b") final int b) {
Preconditions.checkArgument(isValidCMYKValue(c, m, y, k),
"CMYK values must be a float value between 0 and 1 inclusive. " +
"Was (%s, %s, %s, %s).", c, m, y, k);
Preconditions.checkArgument(isValidRGBValue(r, g, b),
"RBG values must be an int value between 0 and 255 inclusive " +
"or -1 to indicate no value. Was (%s, %s, %s).", r, g, b);
this.c = c;
this.m = m;
this.y = y;
this.k = k;
this.r = r;
this.g = g;
this.b = b;
}

private static boolean isValidCMYKValue(final float c, final float m, final float y, final float k) {
return
c >= 0f && c <= 1f &&
m >= 0f && m <= 1f &&
y >= 0f && y <= 1f &&
(k >= 0f && k <= 1f) || k == -1;
}

private static boolean isValidRGBValue(final int r, final int g, final int b) {
return (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255)
|| (r == -1 && g == -1 && b == -1);
}

public float getC() { return c; }
public float getM() { return m; }
public float getY() { return y; }
public float getK() { return k; }
public int getR() { return r; }
public int getG() { return g; }
public int getB() { return b; }

@JsonIgnore
public boolean isRGBAlternatePresent() {
return r > -1;
}

public boolean isPresent() {
return k > -1;
}

}

当我传入以下 JSON 并使用 @RequestBody swatch: Color 时,我实际上得到了一个 Color 对象。在我的故障排除中,我还尝试绑定(bind)到 ListMap[String, ListMap[String, Object]] 以保持键顺序,但 Jackson 最终仍然绑定(bind)到 HashMap

{
"c": 0,
"m": 0,
"y": 0,
"k": 0,
"r": 255,
"g": 255,
"b": 255
}

最佳答案

这个问题的根本原因似乎不是特定于 Scala 的,尽管 Scala 模块目前不支持解决它的方法。

关于此主题的另一个问题表明,在反序列化泛型类型时,Spring 没有向 Jackson 提供完全指定的类型信息; Spring 实际上是告诉 Jackson 反序列化 Map[_,_]

Map JSON array of objects to @RequestBody List<T> using jackson

那里和其他地方提到的解决方法是创建一个你想要的类型的派生类,并将它用作你的方法参数:

class ColorMap extends java.util.HashMap[String,Color]
@RequestMapping(Array("/swatches"))
def setSwatches(@RequestBody swatches: ColorMap) = ...

这适用于 Java 集合,但事实证明 Scala 模块无法正确支持此用例。此缺陷被跟踪为 Issue 122 .

在 Scala 模块实现奇偶校验之前,或者在 Spring 纠正其类型传播之前,您将需要使用 Java 集合的派生类,如上所述。

关于java - 使用 Spring、Scala 和 Jackson 将 JSON 反序列化为 Map[String, SomeJavaObject],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com