Decode#
Static methods that turn JSON text, or a tree from Json.parse, into the typed records: ProjectData, SolveOptions, SchedulingChanges and the rest. Use it to load JSON into the typed API. The class has no instances. It keeps no state between calls, so any thread can call it.
Declaration#
;
Static methods#
fromJson#
public static <T> T
Parses json and decodes the document into a new type instance.
| Name | Type | Description |
|---|---|---|
json |
String |
One JSON document. |
type |
Class<T> |
The record class, such as ProjectData.class, SolveOptions.class or SchedulingChanges.class. Any class is accepted. |
Returns - a new T built from the document.
Errors
| Error | When |
|---|---|
InputException, kind PARSE |
Malformed JSON, a duplicate key, the document null, a value of the wrong JSON type, a missing or null required field, an unknown key on a strict record. |
InputException, kind VALIDATION |
An invalid date string, an unknown enum value, level: true. |
NullPointerException |
json is null. |
InputException.path is null for every error Decode throws.
list#
public static <T> Vec
Parses json as a JSON array and decodes each element into a type instance. Use it for top-level arrays: candidates, slots, requirements.
| Name | Type | Description |
|---|---|---|
json |
String |
A JSON array. |
type |
Class<T> |
The element class, such as Candidate.class. |
Returns - a new Vec<T> in array order.
Errors - InputException (PARSE) when the document isn't an array or an element is null, plus every error of fromJson.
fromValue#
public static <T> T
Decodes an already parsed tree: Map<String, Object>, List<Object>, String, Boolean, Number and null, as Json.parse returns them. Use it when the payload is part of a larger document parsed once.
| Name | Type | Description |
|---|---|---|
value |
Object |
The tree for one record. Nullable. |
type |
Class<T> |
The record class. |
Returns - a new T, or null when value is null.
Errors - the decoding errors of fromJson. There's no syntax check, because the tree is already parsed.
Typed shortcuts#
public static ProjectData
public static SolveOptions
public static Vec
// ... one per type in the declaration
Each record shortcut is fromValue(value, X.class) for its return type. candidates, slots and requirements take a List tree and decode each element, the same way as list.
| Method | Returns |
|---|---|
project |
ProjectData |
projectOptions |
ProjectOptions |
solveOptions |
SolveOptions |
previewOptions |
PreviewOptions |
analyzeOptions |
AnalyzeOptions |
levelingOptions |
LevelingOptions; null for false |
changes |
SchedulingChanges |
evaluateOptions |
EvaluateOptions |
loadProfileOptions |
LoadProfileOptions |
suggestOptions |
SuggestOptions |
pool |
Pool |
candidates |
Vec<Candidate> |
slots |
Vec<Slot> |
requirements |
Vec<Requirement> |
Errors - the decoding errors of fromJson. The list shortcuts also throw InputException (PARSE) when value isn't a List or holds a null element.
Decoding rules#
These rules apply to every method above:
- Keys. A snake_case key binds to the camelCase field (
start_date→startDate,calendar_id→calendarId).typebinds totaskTypeon task records andlinkTypeon link records. On data records (ProjectData,TaskData,LinkDataand the like) the camelCase key binds too, and unknown keys are ignored, so UI fields such astextpass through. Option, change-set and skill records (SolveOptions,SchedulingChanges,Changes,Candidate,Slot,Pool,Requirementand the like) accept only their listed keys. Any other key is aPARSEerror that names the allowed keys. - Required fields. A required field (
tasksonProjectData,idonTaskData,sourceandtargeton links, ...) that's missing ornullis aPARSEerror. nulland missing.nullon an ordinary field leaves the field's default. On aPatchfield,nulldecodes toPatch.nullValue()(clear) and a missing key staysPatch.missing(). Anullelement inside a list, or anullmap value, is aPARSEerror.- Numbers.
intandlongfields need an integral number.intfields accept0to4294967295and cap values aboveInteger.MAX_VALUEatInteger.MAX_VALUE. Ids accept a number or a string. - Enums. Enum values are matched without regard to case, with
-read as_("asap","ASAP","task-inherited"). Spellings that are specific to each enum are on its type page. - Value checks. Decoding checks only JSON types, required fields, dates and enum names. Range and cross-field checks such as
priorityin0..1000run later, when the record reaches aProjectcall.
Example#
;
To return typed results as JSON, use Encode.text. For a full request and response round trip, see Exchange JSON with a client.
Related#
Json- the parser that produces the treesfromValuetakesEncode- writes typed results back as JSONInputException- thrown for undecodable inputProjectData- the main record to decode- Exchange JSON with a client - decoding a request into typed records