Skip to content

Commit

Permalink
Improve documentation for literals in SpEL expressions
Browse files Browse the repository at this point in the history
Closes gh-29701
  • Loading branch information
sbrannen committed Dec 16, 2022
1 parent fdf3bcc commit 9165391
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/docs/asciidoc/core/core-expressions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -744,25 +744,42 @@ topics:
[[expressions-ref-literal]]
=== Literal Expressions

The types of literal expressions supported are strings, numeric values (int, real, hex),
boolean, and null. Strings are delimited by single quotation marks. To put a single quotation mark itself
in a string, use two single quotation mark characters.
SpEL supports the following types of literal expressions.

The following listing shows simple usage of literals. Typically, they are not used
in isolation like this but, rather, as part of a more complex expression -- for example,
using a literal on one side of a logical comparison operator.
- strings
- numeric values: integer (`int` or `long`), hexadecimal (`int` or `long`), real (`float`
or `double`)
- boolean values: `true` or `false`
- null

Strings can delimited by single quotation marks (`'`) or double quotation marks (`"`). To
include a single quotation mark within a string literal enclosed in single quotation
marks, use two adjacent single quotation mark characters. Similarly, to include a double
quotation mark within a string literal enclosed in double quotation marks, use two
adjacent double quotation mark characters.

Numbers support the use of the negative sign, exponential notation, and decimal points.
By default, real numbers are parsed by using `Double.parseDouble()`.

The following listing shows simple usage of literals. Typically, they are not used in
isolation like this but, rather, as part of a more complex expression -- for example,
using a literal on one side of a logical comparison operator or as an argument to a
method.

[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
ExpressionParser parser = new SpelExpressionParser();
// evals to "Hello World"
// evaluates to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
// evaluates to "Tony's Pizza"
String pizzaParlor = (String) parser.parseExpression("'Tony''s Pizza'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
// evals to 2147483647
// evaluates to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
Expand All @@ -774,22 +791,22 @@ using a literal on one side of a logical comparison operator.
----
val parser = SpelExpressionParser()
// evals to "Hello World"
// evaluates to "Hello World"
val helloWorld = parser.parseExpression("'Hello World'").value as String
// evaluates to "Tony's Pizza"
val pizzaParlor = parser.parseExpression("'Tony''s Pizza'").value as String
val avogadrosNumber = parser.parseExpression("6.0221415E+23").value as Double
// evals to 2147483647
// evaluates to 2147483647
val maxValue = parser.parseExpression("0x7FFFFFFF").value as Int
val trueValue = parser.parseExpression("true").value as Boolean
val nullValue = parser.parseExpression("null").value
----

Numbers support the use of the negative sign, exponential notation, and decimal points.
By default, real numbers are parsed by using `Double.parseDouble()`.



[[expressions-properties-arrays]]
Expand All @@ -804,15 +821,15 @@ Pupin's city of birth, we use the following expressions:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
// evals to 1856
// evaluates to 1856
int year = (Integer) parser.parseExpression("birthdate.year + 1900").getValue(context);
String city = (String) parser.parseExpression("placeOfBirth.city").getValue(context);
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// evals to 1856
// evaluates to 1856
val year = parser.parseExpression("birthdate.year + 1900").getValue(context) as Int
val city = parser.parseExpression("placeOfBirth.city").getValue(context) as String
Expand Down

0 comments on commit 9165391

Please sign in to comment.