Giter Club home page Giter Club logo

cable.standaloneconverter's Introduction

Cable.StandaloneConverter

The Cable converter can be used standalone without HTTP abstractions. This project demonstrates that:

Using Cable converter on the server with GET handler:

// server
Get["/get-best-student"] = args =>
{
    var bestStudent = new Student
    {
        Id = 1,
        Level = Level.Advanced,
        Name = "Albert",
        Subjects = new string[] { "Physics" },
        DateOfBirth = DateTime.Now.AddYears(-25)
    };

    return Json.Serialize(bestStudent);
};

then using this little Http class helper you can GET the json from server and deserialize it on the client to a concrete object:

// call the GET handler on the server from client
var bestStudentJson = await Http.GetAsync("/get-best-student");
var bestStudent = Json.Deserialize<Student>(bestStudentJson);

Console.WriteLine($"Student: {bestStudent.Name}"); // logs "Student: Albert"
Console.WriteLine($"Level: {bestStudent.Level}"); // logs "Level: Advanced"
Console.WriteLine($"Birthday: {bestStudent.DateOfBirth.ToString("dd/MM/yyyy")}"); // logs "Birthday: 03/08/1992"

Using Cable on the server with POST handler

here we deserialize the incoming JSON to a concrete object on the server and return (echo) the subjects back:

// server
Post["/echo-student-subjects", runAsync: true] = async (args, ct) =>
{
    using (var streamReader = new StreamReader(Request.Body))
    {
        var requestBodyContents = await streamReader.ReadToEndAsync();
        var student = Json.Deserialize<Student>(requestBodyContents);
        var subjects = student.Subjects;
        return Json.Serialize(subjects);
    }
};

then from the client you can post the data:

// Call the POST handler on the server
var echoedStudentSubjects = await Http.PostAsync("/echo-student-subjects", bestStudentJson);
var studentSubjects = Json.Deserialize<string[]>(echoedStudentSubjects);
foreach(var subject in studentSubjects)
{
    Console.WriteLine(subject);
}

Build the solution

  • Clone the repo and open the solution.
  • Build using Ctrl + Shift + B.
  • Run the ServerSide project
  • Navigate to http://localhost:8080

cable.standaloneconverter's People

Contributors

zaid-ajaj avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.