From 1f391ef53227be4a52356516ba457b89727ecc96 Mon Sep 17 00:00:00 2001 From: minliuhua <290631660@qq.com> Date: Sat, 18 Nov 2023 23:39:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=BF=87=E6=BB=A4=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=AD=97=E6=AE=B5=E7=9A=84=E9=9D=99=E6=80=81=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/noear/snack/ONode.java | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/snack3/src/main/java/org/noear/snack/ONode.java b/snack3/src/main/java/org/noear/snack/ONode.java index f3bcf4f..f5f402e 100644 --- a/snack3/src/main/java/org/noear/snack/ONode.java +++ b/snack3/src/main/java/org/noear/snack/ONode.java @@ -1165,6 +1165,47 @@ public class ONode { return load(source, opts, DEFAULTS.DEF_OBJECT_FROMER).toString(); } + + /** + * 字会串化, 过滤指定字段 (由序列化器决定格式) + * + * @param source java object + * @param filters 需要过滤的字段数组 + * @throws Exception + */ + public static String stringify(Object source, String[] filters) { + return stringify(source, Options.def(), filters); + } + + /** + * 字会串化, 过滤指定字段 (由序列化器决定格式) + * + * @param source java object + * @param opts 常量配置 + * @param filters 需要过滤的字段数组 + * @throws Exception + */ + public static String stringify(Object source, Options opts, String[] filters) { + ONode load = load(source, opts, DEFAULTS.DEF_OBJECT_FROMER); + // 判断是否需要过滤 + if (filters != null && filters.length > 0) { + ONodeData d = load._d; + if (d != null) { + Map object = d.object; + if (object != null && !object.isEmpty()) { + for (String filter : filters) { + object.remove(filter); + } + } + } + } + if (load == null) { + return ""; + } + // 加载java object,须指定Fromer + return load.toString(); + } + //////////////////// // // 序列化 @@ -1250,4 +1291,4 @@ public class ONode { public int hashCode() { return _d.hashCode(); } -} \ No newline at end of file +} -- Gitee