[Canonical page](<https://docs.dhtmlx.com/planning-engine/java/api/json/encode/>)
[Full documentation index](<https://docs.dhtmlx.com/planning-engine/llms.txt>)


# Encode

Converts a typed result, such as a [`ScheduleResult`](<https://docs.dhtmlx.com/planning-engine/java/api/results/schedule-result.md>) or an [`ApplyReport`](<https://docs.dhtmlx.com/planning-engine/java/api/changes/apply-report.md>), into JSON text. The text is the same value that [`Wire`](<https://docs.dhtmlx.com/planning-engine/java/api/json/wire.md>) and [`JsonProject`](<https://docs.dhtmlx.com/planning-engine/java/api/json/json-project.md>) put in the `result` field of a success envelope. All the members you call are static.

## Declaration

```java
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

```java
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`](<https://docs.dhtmlx.com/planning-engine/java/api/json/json.md#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](<https://docs.dhtmlx.com/planning-engine/java/api/json/encode.md#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`](<https://docs.dhtmlx.com/planning-engine/java/api/data/project-data.md>) or a `Map` |

## Accepted values

| Value | JSON |
|---|---|
| Operation results: [`ScheduleResult`](<https://docs.dhtmlx.com/planning-engine/java/api/results/schedule-result.md>), [`ApplyReport`](<https://docs.dhtmlx.com/planning-engine/java/api/changes/apply-report.md>), [`LevelingResult`](<https://docs.dhtmlx.com/planning-engine/java/api/resources/leveling-result.md>), [`EvaluateResult`](<https://docs.dhtmlx.com/planning-engine/java/api/evaluate/evaluate-result.md>), [`SuggestResult`](<https://docs.dhtmlx.com/planning-engine/java/api/suggest/suggest-result.md>), [`LoadProfile`](<https://docs.dhtmlx.com/planning-engine/java/api/resources/load-profile.md>), `StateDoc` | An object, as in the operation's success envelope |
| The records nested inside those results, such as [`Mutation`](<https://docs.dhtmlx.com/planning-engine/java/api/results/mutation.md>), [`Conflict`](<https://docs.dhtmlx.com/planning-engine/java/api/results/conflict.md>), [`Note`](<https://docs.dhtmlx.com/planning-engine/java/api/results/note.md>), [`Slot`](<https://docs.dhtmlx.com/planning-engine/java/api/suggest/slot.md>), [`RejectedOperation`](<https://docs.dhtmlx.com/planning-engine/java/api/changes/rejected-operation.md>), [`LevelingOptions`](<https://docs.dhtmlx.com/planning-engine/java/api/resources/leveling-options.md>) | An object, as it appears inside the parent result |
| [`InputException`](<https://docs.dhtmlx.com/planning-engine/java/api/errors/input-exception.md>) | `{"kind","message","path"}`. `path` is left out when it's `null` |
| [`ProjectException`](<https://docs.dhtmlx.com/planning-engine/java/api/errors/project-exception.md>) | The kebab-case code string, such as `"needs-resync"` |
| Enums ([`TimeUnit`](<https://docs.dhtmlx.com/planning-engine/java/api/data/time-unit.md>), [`CalendarMode`](<https://docs.dhtmlx.com/planning-engine/java/api/data/calendar-mode.md>), [`Basis`](<https://docs.dhtmlx.com/planning-engine/java/api/data/basis.md>), `MutationKind`, `ProjectError`, ...) | The lowercase wire spelling, such as `"day"` |
| [`Id`](<https://docs.dhtmlx.com/planning-engine/java/api/data/id.md>) | A string or a number, matching the id's form |
| [`Moment`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md>) | `"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

```java
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":[...]}
  }
}
```

## Related

- [`Decode`](<https://docs.dhtmlx.com/planning-engine/java/api/json/decode.md>) - the reverse direction, JSON to typed records
- [`Wire`](<https://docs.dhtmlx.com/planning-engine/java/api/json/wire.md>) - wraps encoded results in envelopes
- [`Json`](<https://docs.dhtmlx.com/planning-engine/java/api/json/json.md>) - the writer for plain trees
- [Results as JSON](<https://docs.dhtmlx.com/planning-engine/java/scheduling/reading-results.md#results-as-json>) - writing a schedule result as JSON
