stride3d/stride · error · NotSupportedException

Aliases are not supported in JSON

Error message

Aliases are not supported in JSON

What it means

JsonEventEmitter overrides Emit(AliasEventInfo) to throw NotSupportedException because the JSON format has no concept of anchors/aliases. Serializing an object graph that contains the same object instance referenced twice (which the YAM serializer would emit as an anchor+alias) cannot be represented in JSON by this emitter.

Solutions

  1. Remove duplicate/cyclic references from the graph (use DTO copies) before serializing to JSON
  2. Serialize to YAML instead, which supports anchors and aliases
  3. Configure the serializer to disable reference/alias emission (preserve-references off) so shared instances are serialized by value
  4. Deep-copy shared sub-objects so each is an independent instance

Example fix

// before
var dto = new Dto(); dto.Inner = shared; dto.Other = shared; // alias emitted
serializer.Serialize(jsonEmitter, dto);
// after
dto.Other = new Inner(shared.Value); // independent copy, no alias
Defensive patterns

Strategy: try-catch

Validate before calling

// detect shared references that would require aliases
var seen = new HashSet<object>();
bool hasShared = Visit(graph, o => o is { } x && !x.GetType().IsValueType && !seen.Add(x));

Try / catch

try { serializer.Serialize(jsonEmitter, graph, type); }
catch (NotSupportedException) { throw new InvalidOperationException("Graph contains shared/cyclic references; use YAML or copy sub-objects"); }

Prevention

When it happens

Trigger: Calling Serializer.Serialize with a JSON emitter on an object graph containing a reference cycle or a shared/duplicated object instance that triggers alias emission.

Common situations: Serializing parent/child or DAG-shaped object models (same object referenced from two properties) to JSON; graph cycles after switching output format from YAML to JSON.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/3bf784b9f7181a8e. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/JsonEventEmitter.cs:59

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;

namespace Stride.Core.Yaml.Serialization
{
    internal sealed class JsonEventEmitter : ChainedEventEmitter
    {
        public JsonEventEmitter(IEventEmitter nextEmitter)
            : base(nextEmitter)
        {
        }

        public override void Emit(AliasEventInfo eventInfo)
        {
            throw new NotSupportedException("Aliases are not supported in JSON");
        }

        public override void Emit(ScalarEventInfo eventInfo)
        {
            eventInfo.IsPlainImplicit = true;
            eventInfo.Style = ScalarStyle.Plain;

            var typeCode = eventInfo.SourceValue != null
                ? Type.GetTypeCode(eventInfo.SourceType)
                : TypeCode.Empty;

            switch (typeCode)
            {
                case TypeCode.String:
                case TypeCode.Char:
                    eventInfo.Style = ScalarStyle.DoubleQuoted;
                    break;
                case TypeCode.Empty:

View on GitHub (pinned to 96fad776d2)