Skip to content

Fix CLR toJSON method invocation in JSON.stringify#2292

Merged
lahma merged 4 commits into
mainfrom
copilot/fix-tojson-method-error
Feb 17, 2026
Merged

Fix CLR toJSON method invocation in JSON.stringify#2292
lahma merged 4 commits into
mainfrom
copilot/fix-tojson-method-error

Conversation

Copilot AI commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

CLR classes with toJSON methods threw "No public methods with the specified arguments were found" when serialized via JSON.stringify. JSON.stringify calls toJSON() with no arguments, but the CLR method was exposed as MethodInfoFunction which failed to match the signature.

Changes

  • TypeDescriptor: Detects public parameterless toJSON instance methods during type analysis
  • ObjectWrapper: Wraps detected toJSON as ClrFunction in constructor, following the same pattern as length/dispose special properties
  • Property flags: writable, configurable, non-enumerable (matches Date.prototype.toJSON behavior)

Example

public class MyData
{
    public string Name { get; set; }
    public int Value { get; set; }
    
    public object toJSON()
    {
        return new { name = Name, value = Value };
    }
}

var engine = new Engine();
engine.SetValue("data", new MyData { Name = "test", Value = 42 });

// Previously threw exception, now works:
engine.Evaluate("JSON.stringify(data)"); // {"name":"test","value":42}
Original prompt

This section details on the original issue you should resolve

<issue_title>CLR class with toJSON method throws a "JavaScriptException: No public methods with the specified arguments were found" error on JSON.stringify</issue_title>
<issue_description>

Discussed in #2290

Originally posted by kxs-rgamezdiaz February 16, 2026
I am using Jint 4.6.0.0 and need to add a toJSON method explicitly, which causes a "No public methods with the specified arguments were found" error.

Example:

public sealed class MyClassToJson()
{
    public string toJSON() => "{ \"name\": \"MyClassToJson\" }";
}

[TestMethod]
public void TestToJson() // Passes
{
    Jint.Engine engine = new();
    MyClassToJson myClass = new();
    engine.SetValue("myClass", myClass);
    string result = engine.Evaluate("myClass.toJSON()").ToString();
    Assert.AreEqual(myClass.toJSON(), result);
}

[TestMethod]
public void TestJsonStringify() // Fails
{
    Jint.Engine engine = new();
    MyClassToJson myClass = new();
    engine.SetValue("myClass", myClass);
    string result = engine.Evaluate("JSON.stringify(myClass)").ToString();
    Assert.AreEqual(myClass.toJSON(), result);
}

The TestJsonStringify test fails with this stacktrace:

Message: 
Test method ServerAPI.UnitTest.ScriptRuntimeV2.JintTests.TestJsonStringify threw exception: 
Jint.Runtime.JavaScriptException: No public methods with the specified arguments were found. ---> 
Jint.Runtime.JavaScriptException+JavaScriptErrorWrapperException: No public methods with the specified arguments were found.

  Stack Trace: 
stringify (<anonymous>:1:16)
<anonymous>:1:1
--- End of inner exception stack trace ---
Engine.ScriptEvaluation(ScriptRecord scriptRecord, ParserOptions parserOptions)
Engine.ExecuteWithConstraints[T](Boolean strict, Func`1 callback)
Engine.Execute(Prepared`1& preparedScript)
Engine.Evaluate(String code, String source)
JintTests.TestJsonStringify() line 55

Is this a bug or limitation in Jint? Is there a workaround? I need to have a toJSON method on my js class. Thank you for your help.

</issue_description>

<agent_instructions>Our TypeDescriptor should contain information whether type knows how to create toJSON description and it should be integrated the same way as ObjectWrapper already adds supported features based on object's shape.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits February 17, 2026 20:13
…pper

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
…e it non-enumerable

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CLR class with toJSON method causing JavaScriptException Fix CLR toJSON method invocation in JSON.stringify Feb 17, 2026
Copilot AI requested a review from lahma February 17, 2026 20:27
@lahma
lahma marked this pull request as ready for review February 17, 2026 20:55
@lahma
lahma merged commit 71357f1 into main Feb 17, 2026
4 checks passed
@lahma
lahma deleted the copilot/fix-tojson-method-error branch February 17, 2026 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLR class with toJSON method throws a "JavaScriptException: No public methods with the specified arguments were found" error on JSON.stringify

2 participants