Java 从解析的JSON中排除/删除字段

Java 从解析的JSON中排除/删除字段,java,json,Java,Json,我正在调用一个传递JSON并返回JSON的web服务。我正在尝试记录请求和响应,以便进行故障排除。我想排除或划掉(通过*)由字段名regex或其他东西标识的任何密码值。我正在使用Gson进行JSON解析和序列化。在我的logRequest和logResponse方法中包括以下toJson方法调用: private String toJson(String str) { JsonElement elt = JSON_PARSER.parse(str); return GSON.to

我正在调用一个传递JSON并返回JSON的web服务。我正在尝试记录请求和响应,以便进行故障排除。我想排除或划掉(通过*)由字段名regex或其他东西标识的任何密码值。我正在使用Gson进行JSON解析和序列化。在我的
logRequest
logResponse
方法中包括以下
toJson
方法调用:

private String toJson(String str) {
    JsonElement elt = JSON_PARSER.parse(str);
    return GSON.toJson(elt);
}

JsonParser
对象上,我看不到任何对我有帮助的东西。在通过
GsonBuilder
构建
Gson
实例时,我尝试了各种方法,但都没有成功。这种方法的困难似乎在于,我没有映射到POJO,这将允许我使用
排除策略。我目前的想法是递归地检查
jsonement
我从
parse
方法得到的结果,但我不确定这是否会起作用,而且感觉很难,所以我想我会问。

由于通用JSON序列化的工作方式,以通用方式实现您的需求将很困难。这里已经有人问了一个类似的问题:。 在解析JSON字符串后遍历JSON对象,识别密码字段,并在序列化回日志程序的字符串之前显式清除值,如果您愿意遵循此路径,这似乎是一个不错的选择

但是,如果您知道要记录的文档的json模式,那么问题可以更容易地解决。在这种情况下,您可以使用jsonschema2pojo maven插件从模式生成Java Pojo对象,然后使用带有序列化排除策略的Gson库。以下是一个例子:

    String jsonString = "{\"name\":\"parent\",\"id\":\"parentId\",\"password\":\"topsecret\"" +
            ",\"childPojo\":{\"name\":\"child\",\"id\":\"childId\",\"password\":\"topsecret\"}}";

    RegexFieldExclusionStrategy strategy = new RegexFieldExclusionStrategy("pass.*");

    Gson gson = new GsonBuilder()
            .addSerializationExclusionStrategy(strategy)
            .create();

    MyPojo myPojo = gson.fromJson(jsonString, MyPojo.class);

    String json = gson.toJson(myPojo);
    System.out.println(json);
MyPojo课程:

public class MyPojo {

  private String name;
  private String id;
  private String password;
  private MyPojo childPojo;

  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }
  public MyPojo getChildPojo() {
      return childPojo;
  }
  public void setChildPojo(MyPojo childPojo) {
      this.childPojo = childPojo;
  }
}
import java.util.regex.Pattern;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class RegexFieldExclusionStrategy implements ExclusionStrategy {

  private String regex;

  public RegexFieldExclusionStrategy(String regex) {
      Pattern.compile(regex);
      this.regex = regex;
  }

  public boolean shouldSkipClass(Class<?> f) {
      return false;
  }

  public boolean shouldSkipField(FieldAttributes f) {
      return f.getName().toLowerCase().matches(regex);
  }
}
注意:这个
Pojo
是一个手动实现,可以使用上面提到的插件替换为生成的,以简化整个过程

RegexFieldExclutionStrategy类:

public class MyPojo {

  private String name;
  private String id;
  private String password;
  private MyPojo childPojo;

  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }
  public MyPojo getChildPojo() {
      return childPojo;
  }
  public void setChildPojo(MyPojo childPojo) {
      this.childPojo = childPojo;
  }
}
import java.util.regex.Pattern;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class RegexFieldExclusionStrategy implements ExclusionStrategy {

  private String regex;

  public RegexFieldExclusionStrategy(String regex) {
      Pattern.compile(regex);
      this.regex = regex;
  }

  public boolean shouldSkipClass(Class<?> f) {
      return false;
  }

  public boolean shouldSkipField(FieldAttributes f) {
      return f.getName().toLowerCase().matches(regex);
  }
}

由于通用JSON序列化的工作方式,以通用方式实现您的需求将非常困难。这里已经有人问了一个类似的问题:。 在解析JSON字符串后遍历JSON对象,识别密码字段,并在序列化回日志程序的字符串之前显式清除值,如果您愿意遵循此路径,这似乎是一个不错的选择

但是,如果您知道要记录的文档的json模式,那么问题可以更容易地解决。在这种情况下,您可以使用jsonschema2pojo maven插件从模式生成Java Pojo对象,然后使用带有序列化排除策略的Gson库。以下是一个例子:

    String jsonString = "{\"name\":\"parent\",\"id\":\"parentId\",\"password\":\"topsecret\"" +
            ",\"childPojo\":{\"name\":\"child\",\"id\":\"childId\",\"password\":\"topsecret\"}}";

    RegexFieldExclusionStrategy strategy = new RegexFieldExclusionStrategy("pass.*");

    Gson gson = new GsonBuilder()
            .addSerializationExclusionStrategy(strategy)
            .create();

    MyPojo myPojo = gson.fromJson(jsonString, MyPojo.class);

    String json = gson.toJson(myPojo);
    System.out.println(json);
MyPojo课程:

public class MyPojo {

  private String name;
  private String id;
  private String password;
  private MyPojo childPojo;

  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }
  public MyPojo getChildPojo() {
      return childPojo;
  }
  public void setChildPojo(MyPojo childPojo) {
      this.childPojo = childPojo;
  }
}
import java.util.regex.Pattern;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class RegexFieldExclusionStrategy implements ExclusionStrategy {

  private String regex;

  public RegexFieldExclusionStrategy(String regex) {
      Pattern.compile(regex);
      this.regex = regex;
  }

  public boolean shouldSkipClass(Class<?> f) {
      return false;
  }

  public boolean shouldSkipField(FieldAttributes f) {
      return f.getName().toLowerCase().matches(regex);
  }
}
注意:这个
Pojo
是一个手动实现,可以使用上面提到的插件替换为生成的,以简化整个过程

RegexFieldExclutionStrategy类:

public class MyPojo {

  private String name;
  private String id;
  private String password;
  private MyPojo childPojo;

  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }
  public MyPojo getChildPojo() {
      return childPojo;
  }
  public void setChildPojo(MyPojo childPojo) {
      this.childPojo = childPojo;
  }
}
import java.util.regex.Pattern;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class RegexFieldExclusionStrategy implements ExclusionStrategy {

  private String regex;

  public RegexFieldExclusionStrategy(String regex) {
      Pattern.compile(regex);
      this.regex = regex;
  }

  public boolean shouldSkipClass(Class<?> f) {
      return false;
  }

  public boolean shouldSkipField(FieldAttributes f) {
      return f.getName().toLowerCase().matches(regex);
  }
}