当值为数字时,Jettison使用json编组将字符串数据类型转换为整数类型

时间:2021-07-03 15:29:38

We are using jettison-1.3.3 for converting JaxB to Json.

我们使用jettison-1.3.3将JaxB转换为Json。

We are facing an issue. whenever I have a String property that contains all numbers (i.e. String phone="12345";), the JSON response will display it as a number (12345) without having the double quotes.

我们正面临一个问题。每当我有一个包含所有数字的String属性(即String phone =“12345”;)时,JSON响应将显示为一个数字(12345)而没有双引号。

If the value is coming as 1234AS in this case returns with double quote. How to fix this and making sure its always has double quotes.

如果值为1234AS,则在此情况下返回双引号。如何解决这个问题并确保它始终具有双引号。

Please help

3 个解决方案

#1


3  

There are type converters in the jettison. By default it uses default type converter. Default type convert removes double quotes if the value is numeric type.

放射中有类型转换器。默认情况下,它使用默认类型转换器如果值为数字类型,则默认类型convert将删除双引号。

To get always double quotes use SimpleConverter.

要始终使用双引号,请使用SimpleConverter。

Create a system property - i.e System.setProperty("jettison.mapped.typeconverter.class", "org.codehaus.jettison.mapped.SimpleConverter");

创建一个系统属性 - 即System.setProperty(“jettison.mapped.typeconverter.class”,“org.codehaus.jettison.mapped.SimpleConverter”);

So jettison uses simple converter and output values as string.

所以jettison使用简单的转换器和输出值作为字符串。

#2


2  

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB(JSR-222)专家组的成员。

You could use the JSON-binding offered by MOXy for this use case.

您可以使用MOXy提供的JSON绑定来处理此用例。

Domain Model (Root)

域模型(根)

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    int number;
    String string;

}

Specifying MOXy as your JSON-Binding Provider

将MOXy指定为JSON绑定提供程序

In a RESTful environment you can specify MOXyJsonProvider as the MessageBodyReader/MessageBodyWriter for your JAX-RS application

在RESTful环境中,您可以将MOXyJsonProvider指定为JAX-RS应用程序的MessageBodyReader / MessageBodyWriter

In the standalone example below you can specify a jaxb.properties file in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

在下面的独立示例中,您可以使用以下条目在与域模型相同的包中指定jaxb.properties文件(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as- your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo Code

Below is a standalone example you can run to prove that everything works:

下面是一个独立的示例,您可以运行以证明一切正常:

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);

        Root root = new Root();
        root.number = 1234;
        root.string = "1234";

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

Output

Below is the output from running the demo code:

以下是运行演示代码的输出:

{
   "number" : 1234,
   "string" : "1234"
}

#3


1  

This seems to be an implicit "feature" of Jettison; it tries to introspect the actual data and figure out what's the best type fit. Better try using some other libraries such as Jackson. Jackson does not try to provide some unnecessary help causing such issues.

这似乎是Jettison隐含的“特征”;它试图反省实际数据并找出最适合的类型。最好尝试使用其他一些库,比如杰克逊。杰克逊并没有试图提供一些不必要的帮助来解决这些问题。

#1


3  

There are type converters in the jettison. By default it uses default type converter. Default type convert removes double quotes if the value is numeric type.

放射中有类型转换器。默认情况下,它使用默认类型转换器如果值为数字类型,则默认类型convert将删除双引号。

To get always double quotes use SimpleConverter.

要始终使用双引号,请使用SimpleConverter。

Create a system property - i.e System.setProperty("jettison.mapped.typeconverter.class", "org.codehaus.jettison.mapped.SimpleConverter");

创建一个系统属性 - 即System.setProperty(“jettison.mapped.typeconverter.class”,“org.codehaus.jettison.mapped.SimpleConverter”);

So jettison uses simple converter and output values as string.

所以jettison使用简单的转换器和输出值作为字符串。

#2


2  

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB(JSR-222)专家组的成员。

You could use the JSON-binding offered by MOXy for this use case.

您可以使用MOXy提供的JSON绑定来处理此用例。

Domain Model (Root)

域模型(根)

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    int number;
    String string;

}

Specifying MOXy as your JSON-Binding Provider

将MOXy指定为JSON绑定提供程序

In a RESTful environment you can specify MOXyJsonProvider as the MessageBodyReader/MessageBodyWriter for your JAX-RS application

在RESTful环境中,您可以将MOXyJsonProvider指定为JAX-RS应用程序的MessageBodyReader / MessageBodyWriter

In the standalone example below you can specify a jaxb.properties file in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

在下面的独立示例中,您可以使用以下条目在与域模型相同的包中指定jaxb.properties文件(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as- your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo Code

Below is a standalone example you can run to prove that everything works:

下面是一个独立的示例,您可以运行以证明一切正常:

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);

        Root root = new Root();
        root.number = 1234;
        root.string = "1234";

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

Output

Below is the output from running the demo code:

以下是运行演示代码的输出:

{
   "number" : 1234,
   "string" : "1234"
}

#3


1  

This seems to be an implicit "feature" of Jettison; it tries to introspect the actual data and figure out what's the best type fit. Better try using some other libraries such as Jackson. Jackson does not try to provide some unnecessary help causing such issues.

这似乎是Jettison隐含的“特征”;它试图反省实际数据并找出最适合的类型。最好尝试使用其他一些库,比如杰克逊。杰克逊并没有试图提供一些不必要的帮助来解决这些问题。