Encode#

Converts a typed result, such as a ScheduleResult or an ApplyReport, into JSON text. The text is the same value that Wire and JsonProject put in the result field of a success envelope. All the members you call are static.

Declaration#

package org.dhtmlx.plen;

public class Encode extends ValueRecord {
  public static String text(Object result)
}

The class also has public static writers that take a WireWriter (write, schedule, apply, mutation, ...). WireWriter is package-private, so code outside org.dhtmlx.plen can't call them. Use text.

Static methods#

text#

public static String text(Object result)

Returns the JSON text of result. Object keys are sorted. Optional fields that are null on the record are left out. Numbers and dates are written as Json.stringify writes them, and dates come out as "YYYY-MM-DD HH:mm".

Parameters

Name Type Description
result Object The value to encode. See Accepted values. null gives "null".

Returns the JSON text, without an envelope. To wrap it as {"ok":true,"result":...}, call Wire.resultText(result).

Errors

Error When
IllegalStateException A number or numeric id in result is NaN or infinite
UnsupportedOperationException result, or an element of an Iterable, is a type with no encoder, such as ProjectData or a Map

Accepted values#

Value JSON
Operation results: ScheduleResult, ApplyReport, LevelingResult, EvaluateResult, SuggestResult, LoadProfile, StateDoc An object, as in the operation's success envelope
The records nested inside those results, such as Mutation, Conflict, Note, Slot, RejectedOperation, LevelingOptions An object, as it appears inside the parent result
InputException {"kind","message","path"}. path is left out when it's null
ProjectException The kebab-case code string, such as "needs-resync"
Enums (TimeUnit, CalendarMode, Basis, MutationKind, ProjectError, ...) The lowercase wire spelling, such as "day"
Id A string or a number, matching the id's form
Moment "YYYY-MM-DD HH:mm"
String, Character, Boolean, Double, Byte, Integer, Long A JSON string, boolean or number
Iterable (Vec, List, ...) An array, with each element encoded by the same rules

Example#

import java.util.List;
import org.dhtmlx.plen.*;

public class EncodeExample {
  public static void main(String[] args) throws Exception {
    Moment monday = Moment.fromYmdHm(2026, 1, 5, 8, 0);

    ProjectData data = new ProjectData()
        .withTasks(List.of(
            new TaskData(Id.str("design")).withStartDate(monday).withDuration(2.0),
            new TaskData(Id.str("build")).withStartDate(monday).withDuration(3.0)))
        .withLinks(List.of(
            new LinkData().withId(Id.str("l1"))
                .withSource(Id.str("design"))
                .withTarget(Id.str("build"))));

    ScheduleResult result = Engine.schedule(data, new SolveOptions());

    String json = Encode.text(result);
    System.out.println(json);
    // {"conflicts":[],"converged":true,"cycles":[],"notes":[],"updates":[...]}
  }
}
  • Decode - the reverse direction, JSON to typed records
  • Wire - wraps encoded results in envelopes
  • Json - the writer for plain trees
  • Results as JSON - writing a schedule result as JSON