防止某些字段被序列化

时间:2022-11-11 22:16:39

In the Play framework i have a few models that have fields which are object references to other models. When i use renderJSON, i don't want those object references to be included. Currently for my needs i create a separate view model class which contains the fields i want, and in the controller i create instances of this view class as needed. Ideally i would like to be able to use the model class itself without having to write the view class.

在Play框架中,我有一些模型,其中的字段是对其他模型的对象引用。当我使用renderJSON时,我不希望包含这些对象引用。目前,根据我的需要,我创建了一个单独的视图模型类,其中包含我想要的字段,并在控制器中根据需要创建此视图类的实例。理想情况下,我希望能够使用模型类本身,而无需编写视图类。

Is there a way to annotate a field so that it will not be serialized when using renderJSON?

有没有办法注释一个字段,以便在使用renderJSON时不会序列化?

4 个解决方案

#1


11  

because play uses Gson for its Json serialization you can try the following:

因为Play使用Gson进行Json序列化,你可以尝试以下方法:

public static void test()  
{  
    Object foo = new SomeObject("testData");  
    Gson gson = new GsonBuilder()
        .excludeFieldsWithModifiers(Modifier.TRANSIENT)  
        .create();
    renderJSON(gson.toJson(foo));  
}

now each field marked as transient will not be serialized. There is also another (better) way. You can use the com.google.gson.annotations.Expose annotation to mark each field you want to serialize.

现在标记为瞬态的每个字段都不会被序列化。还有另一种(更好的)方式。您可以使用com.google.gson.annotations.Expose注释标记要序列化的每个字段。

public static void test()  
{  
    Object foo = new SomeObject("testData");  
    Gson gson = new GsonBuilder()
        .excludeFieldsWithoutExposeAnnotation()  
        .create();
    renderJSON(gson.toJson(foo));  
}

#2


2  

Using FlexJSON with play is another option, explained in this article: http://www.lunatech-research.com/archives/2011/04/20/play-framework-better-json-serialization-flexjson

使用FlexJSON与play是另一种选择,在本文中进行了解释:http://www.lunatech-research.com/archives/2011/04/20/play-framework-better-json-serialization-flexjson

#3


0  

Not sure why no one has written the most direct solution to this answer so I will do it here:

不知道为什么没有人为这个答案写出最直接的解决方案,所以我会在这里做:

Simply mark the fields you do not want serialized via Gson as transient.

只需将您不希望通过Gson序列化的字段标记为瞬态。

Here's an example:

这是一个例子:

public class Animal
{
        private String name = "dog";
        transient private int port = 80;
        private String species = "canine";
        transient private String password = "NoOneShouldSeeThis";
}

None of the items which are marked transient will be serialized. When deserialized they will be set to their default (class default) values.

没有标记为瞬态的项目将被序列化。反序列化时,它们将设置为默认值(类默认值)。

Resulting JSON will look like the following:

生成的JSON将如下所示:

{"name":"dog","species":"canine"}

For more information on transient you can see the SO Why does Java have transient fields?

有关瞬态的更多信息,您可以看到SO为什么Java有瞬态字段?

#4


-1  

I would override renderJSON to check a the field name against a member array of serialization exclusions.

我将覆盖renderJSON以针对序列化排除的成员数组检查字段名称。

#1


11  

because play uses Gson for its Json serialization you can try the following:

因为Play使用Gson进行Json序列化,你可以尝试以下方法:

public static void test()  
{  
    Object foo = new SomeObject("testData");  
    Gson gson = new GsonBuilder()
        .excludeFieldsWithModifiers(Modifier.TRANSIENT)  
        .create();
    renderJSON(gson.toJson(foo));  
}

now each field marked as transient will not be serialized. There is also another (better) way. You can use the com.google.gson.annotations.Expose annotation to mark each field you want to serialize.

现在标记为瞬态的每个字段都不会被序列化。还有另一种(更好的)方式。您可以使用com.google.gson.annotations.Expose注释标记要序列化的每个字段。

public static void test()  
{  
    Object foo = new SomeObject("testData");  
    Gson gson = new GsonBuilder()
        .excludeFieldsWithoutExposeAnnotation()  
        .create();
    renderJSON(gson.toJson(foo));  
}

#2


2  

Using FlexJSON with play is another option, explained in this article: http://www.lunatech-research.com/archives/2011/04/20/play-framework-better-json-serialization-flexjson

使用FlexJSON与play是另一种选择,在本文中进行了解释:http://www.lunatech-research.com/archives/2011/04/20/play-framework-better-json-serialization-flexjson

#3


0  

Not sure why no one has written the most direct solution to this answer so I will do it here:

不知道为什么没有人为这个答案写出最直接的解决方案,所以我会在这里做:

Simply mark the fields you do not want serialized via Gson as transient.

只需将您不希望通过Gson序列化的字段标记为瞬态。

Here's an example:

这是一个例子:

public class Animal
{
        private String name = "dog";
        transient private int port = 80;
        private String species = "canine";
        transient private String password = "NoOneShouldSeeThis";
}

None of the items which are marked transient will be serialized. When deserialized they will be set to their default (class default) values.

没有标记为瞬态的项目将被序列化。反序列化时,它们将设置为默认值(类默认值)。

Resulting JSON will look like the following:

生成的JSON将如下所示:

{"name":"dog","species":"canine"}

For more information on transient you can see the SO Why does Java have transient fields?

有关瞬态的更多信息,您可以看到SO为什么Java有瞬态字段?

#4


-1  

I would override renderJSON to check a the field name against a member array of serialization exclusions.

我将覆盖renderJSON以针对序列化排除的成员数组检查字段名称。