Expression Framework

Easy to get value from Object, Array or Map.

License

License

GroupId

GroupId

com.github.developframework
ArtifactId

ArtifactId

expression
Last Version

Last Version

1.5.1
Release Date

Release Date

Type

Type

jar
Description

Description

Expression Framework
Easy to get value from Object, Array or Map.
Project URL

Project URL

https://github.com/developframework/expression
Source Code Management

Source Code Management

https://github.com/developframework/expression

Download expression

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.developframework/expression/ -->
<dependency>
    <groupId>com.github.developframework</groupId>
    <artifactId>expression</artifactId>
    <version>1.5.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.developframework/expression/
implementation 'com.github.developframework:expression:1.5.1'
// https://jarcasting.com/artifacts/com.github.developframework/expression/
implementation ("com.github.developframework:expression:1.5.1")
'com.github.developframework:expression:jar:1.5.1'
<dependency org="com.github.developframework" name="expression" rev="1.5.1">
  <artifact name="expression" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.developframework', module='expression', version='1.5.1')
)
libraryDependencies += "com.github.developframework" % "expression" % "1.5.1"
[com.github.developframework/expression "1.5.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.projectlombok : lombok Optional jar 1.18.12
org.apache.commons : commons-lang3 jar 3.9

Project Modules

There are no modules declared in this project.

Expression说明文档

1. 概述

用于方便的从对象、数组、Map等结构中获取值

1.1. 引用

<dependency>
	<groupId>com.github.developframework</groupId>
	<artifactId>expression</artifactId>
	<version>${version.expression}</version>
</dependency>

1.2. 依赖项

  • commons-lang3.jar
  • slf4j-api.jar
  • lombok.jar

2. 简单示例

2.1. 表达式对象

Expression expression = Expression.parse("user.name");

把字符串user.name 解析成一个表达式对象。

可以解析的表达式字符串有:

表达式字符串 描述
user.name 从根对象(根对象是对象或Map)取得user实例的name属性
users[0].name 从根对象(根对象是对象或Map)取得users数组实例的第1个元素的name属性
[0].name 从根对象(根对象是数组)取得第1个元素的name属性

2.2. 取值

例如有如下结构的User实体类:

@Data
public class User {

    private String name;

    private String[] emails;

    public User(String name, String[] emails) {
        this.name = name;
        this.emails = emails;
    }
}

有如下的数据结构关系:

User peter = new User("Peter", new String[]{"[email protected]", "[email protected]"});
User tom = new User("Tom", new String[]{"[email protected]", "[email protected]"});
User[] users = new User[]{peter, tom};
Map<String, Object> map = new HashMap<>();
map.put("users", users);

说明:peter,tom两个User对象存放于users数组对象中,而users数组对象存放于map中,键值为"users"。

那么假如要从map中取得peter的第二个email值。普通java代码如下:

String peterSecondEmail = ((User[])map.get("users"))[0].getMails()[1];

列为表达式:users[0].emails[1]

String expressionValue = "users[0].emails[1]";
String peterSecondEmail = ExpressionUtils.getValue(map, expressionValue, String.class);
System.out.println(peterSecondEmail);

Versions

Version
1.5.1
1.5.0
1.4.0
1.3.2
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.1
1.0.0