As of version 1.3.2, the preferred way to specify nested filters is to use square brackets intead of braces.
Preferred: assignee[firstName]
No longer Preferred but will still work: assignee{firstName}
The reason for this is that newer versions of Tomcat no longer allow braces to be specified on the url without being escaped. Square brackets are still permitted in the url and it is preferred to make the syntax url friendly.
The Squiggly Filter is a Jackson JSON PropertyFilter, which selects properties of an object/list/map using a subset of the Facebook Graph API filtering syntax.
Probably the most common use of this library is to filter fields on the querystring like so:
?fields=id,reporter[firstName]
Integrating Squiggly into your webapp is covered in Custom Integration.
<dependency>
<groupId>com.github.bohnman</groupId>
<artifactId>squiggly-filter-jackson</artifactId>
<version>1.3.18</version>
</dependency>
ObjectMapper objectMapper = Squiggly.init(new ObjectMapper(), "assignee{firstName}");
Issue object = new Issue(); // replace this with your object/collection/map here
System.out.println(SquigglyUtils.stringify(objectMapper, object));
Alternatively, if you need more control over configuring the ObjectMapper, you can do it this way:
String filterId = SquigglyPropertyFilter.FILTER_ID;
SquigglyPropertyFilter propertyFilter = new SquigglyPropertyFilter("assignee[firstName]"); // replace with your filter here
SimpleFilterProvider filterProvider = new SimpleFilterProvider().addFilter(filterId, propertyFilter);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setFilterProvider(filterProvider);
objectMapper.addMixIn(Object.class, SquigglyPropertyFilterMixin.class);
Issue object = new Issue(); // replace this with your object/collection/map here
System.out.println(SquigglyUtils.stringify(objectMapper, object));
Also, you can generate a Plain Old Java Object (POJO) instead of a JSON String
ObjectMapper objectMapper = Squiggly.init(new ObjectMapper(), "assignee{firstName}");
System.out.println(SquigglyUtils.objectify(objectMapper, object, Object.class));
For applying filter on Collection of Objects and for returning Collection of POJOs instead of JSON String
List<User> users = Arrays.asList(
new User("Peter", 12, "Dinklage"),
new User("Lena", 13, "Heady"));
String filter = "firstName,age";
ObjectMapper objectMapper = Squiggly.init(new ObjectMapper(), filter);
List<User> filteredUsers = SquigglyUtils.listify(objectMapper, users, User.class);
// setify is also availble
For the filtering examples, let's use an the example object of type Issue
{
"id": "ISSUE-1",
"issueSummary": "Dragons Need Fed",
"issueDetails": "I need my dragons fed pronto.",
"reporter": {
"firstName": "Daenerys",
"lastName": "Targaryen"
},
"assignee": {
"firstName": "Jorah",
"lastName": "Mormont"
},
"actions": [
{
"id": null,
"type": "COMMENT",
"text": "I'm going to let Daario get this one.",
"user": {
"firstName": "Jorah",
"lastName": "Mormont"
}
},
{
"id": null,
"type": "CLOSE",
"text": "All set.",
"user": {
"firstName": "Daario",
"lastName": "Naharis"
}
}
],
"properties": {
"priority": "1",
"email": "motherofdragons@got.com"
}
}
String filter = "";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {}
filter = "id";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"id":"ISSUE-1"}
filter = "id,issueSummary"
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"id":"ISSUE-1", "issueSummary":"Dragons Need Fed"}
filter = "issue*";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"issueSummary":"Dragons Need Fed", "issueDetails": "I need my dragons fed pronto."}
filter = "**";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints the same json as our example object
filter = "*";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
/* prints the following:
{
"id": "ISSUE-1",
"issueSummary": "Dragons Need Fed",
"issueDetails": "I need my dragons fed pronto.",
"reporter": {
"firstName": "Daenerys",
"lastName": "Targaryen"
},
"assignee": {
"firstName": "Jorah",
"lastName": "Mormont"
},
"actions": [
{
"id": null,
"type": "COMMENT",
"text": "I'm going to let Daario get this one.."
},
{
"id": null,
"type": "CLOSE",
"text": "All set."
}
],
"properties": {
"priority": "1",
"email": "motherofdragons@got.com"
}
}
*/
String filter = "assignee[firstName]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"assignee":{"firstName":"Jorah"}}
String filter = "actions[text,type]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"actions":[{"type":"COMMENT","text":"I'm going to let Daario get this one.."},{"type":"CLOSE","text":"All set."}]}
// NOTE: use can also use wildcards (e.g. actions{t*})
String filter = "(assignee,reporter)[firstName]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"reporter":{"firstName":"Daenerys"},"assignee":{"firstName":"Jorah"}}
String filter = "actions[user[lastName]]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"actions":[{"user":{"lastName":"Mormont"}},{"user":{"lastName":"Naharis"}}]}
As an alternative to using the braces syntax for nested filter, you can use the dot syntax
String filter = "assignee.firstName";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"assignee":{"firstName":"Jorah"}}
You can exclude fields using the dot syntax. Note that the exclusion applies to the last field.
String filter = "-assignee.firstName";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"assignee":{"lastName":"Mormont"}}
You can also combine the dot syntax with the nested syntax.
String filter = "actions.user[firstName]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"actions":[{"user":{"firstName":"Jorah"}},{"user":{"firstName":"Daario"}}]}
One limitation is that you cannot use the | syntax with the dot syntax
String filter = "(actions.user,assignee)[firstName]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// throws exception
In addition to using wildcards, you can also use regular expressions.
Here's an example:
String filter = "~iss[a-z]e.*~";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"issueSummary":"Dragons Need Fed","issueDetails":"I need my dragons fed pronto."}
Notice the tildes mark the begin and of the regex pattern.
You can also specifiy a case insensitive match.
String filter = "~iss[a-z]esumm.*~i";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"issueSummary":"Dragons Need Fed"}
Why use tildes and not forward slashes for regular expressions? Tildes are query string friendly and forward slashes are not.
However, you may use forward slashes if you like.
String filter = "/iss[a-z]esumm.*/i";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints {"issueSummary":"Dragons Need Fed"}
Selecting from maps is the same as selecting from objects. Instead of selecting from fields, you are selecting from keys. The main downside of selecting from maps that their matches are unable to be cached.
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("bear", "baz");
String filter = "foo";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, map));
// prints {"foo":"bar"}
Selecting from collection just assumes the top-level objects are the elements in the collection, not the collection itself.
List<User> list = Arrays.asList(
new User("Peter", "Dinklage"),
new User("Lena", "Heady")
);
String filter = "firstName";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, list));
// prints [{"firstName":"Peter"}, {"firstName":"Lena"}]
When a filter includes two criteria that match the same field, the one that is more specific wins.
For example, if the filter is "**,reporter[firstName]", then all fields will be excluded. However, the reporter field will only include the firstName field.
Specificity is determined using the following logic:
In order to exclude fields, you need to prefix the field name with a minus sign (-).
Let's look at an example
String filter = "-reporter";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints everything except the reporter field
Here's an example excluding a nested field:
String filter = "**,reporter[-firstName]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// prints everything, the reporter object will only have the firstName excluded
NOTE: Excluded fields can't have nested filters
String filter = "**,-reporter[firstName]";
ObjectMapper mapper = Squiggly.init(mapper, filter);
System.out.println(SquigglyUtils.stringify(mapper, issue));
// throws an exception
In addition to selecting fields by name, you can assign a name to a group of fields. This is called a property view.
Let's use these reference objects for the examples.
@Target(FIELD)
@Retention(RUNTIME)
@Documented
@PropertyView({"super"})
public @interface SuperView {
}
class Address {
String line1 = "55 Hollywood Blvd.";
String line2 = "";
String city = "Hollywood";
String state = "CA";
@SuperView
double lat;
@SuperView
double lon;
}
class User {
String firstName = "Peter";
String lastName = "Dinklage";
@PropertyView("secret")
String phone = "555-555-1212";
@SuperView
Address address;
}
If nothing is annotated on a field, it is assumed to belong to the "base" view. There is @BaseView convenience annotation, but it's not needed. See Changing Defaults to alter this behavior.
In the case of User, fields firstName and lastName
$ claude mcp add squiggly \
-- python -m otcore.mcp_server <graph>